feat: implement encryption
This commit is contained in:
72
src/main.c
72
src/main.c
@@ -1,18 +1,78 @@
|
||||
#define TINYRISCV_M
|
||||
|
||||
#include "tinyriscv.h"
|
||||
#include "elf.h"
|
||||
#include "hwinfo.h"
|
||||
#include "util.h"
|
||||
|
||||
#include "aes.h"
|
||||
#include "tinyriscv.h"
|
||||
#include "config.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
|
||||
#define MAX_STEPS 1024u
|
||||
|
||||
|
||||
int main(){
|
||||
hw_info_t hwinfo = {0};
|
||||
if(get_hw_info(&hwinfo) != 0){
|
||||
return 1;
|
||||
}
|
||||
|
||||
uint8_t* key = hwinfo_to_key(hwinfo);
|
||||
|
||||
if(key == NULL) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
fprintf(stderr, "HWINFO: %s\n", hw_info_flatten(hwinfo));
|
||||
|
||||
fprintf(stderr, "Key: ");
|
||||
for (int i = 0; i < 16; i++)
|
||||
fprintf(stderr, "%02X", key[i]);
|
||||
fprintf(stderr, "\n");
|
||||
|
||||
|
||||
|
||||
struct AES_ctx ctx;
|
||||
uint8_t nonce[16] = {0};
|
||||
|
||||
FILE* license = fopen(EMU_PROGRAM_PATH, "rb");
|
||||
fread(nonce, 16, 1, license);
|
||||
long data_start = ftell(license);
|
||||
fseek(license, 0, SEEK_END);
|
||||
int license_size = ftell(license) - data_start;
|
||||
fseek(license, data_start, SEEK_SET);
|
||||
uint8_t* buf = malloc(license_size);
|
||||
fread(buf, 1, license_size, license);
|
||||
|
||||
|
||||
// void AES_init_ctx(struct AES_ctx* ctx, const uint8_t* key);
|
||||
// void AES_init_ctx_iv(struct AES_ctx* ctx, const uint8_t* key, const uint8_t* iv);
|
||||
// void AES_CTR_xcrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, size_t length);
|
||||
|
||||
AES_init_ctx(&ctx, key);
|
||||
AES_init_ctx_iv(&ctx, key, nonce);
|
||||
AES_CTR_xcrypt_buffer(&ctx, buf, license_size);
|
||||
|
||||
FILE* decrypted_license = tmpfile();
|
||||
fwrite(buf, 1, license_size, decrypted_license);
|
||||
rewind(decrypted_license);
|
||||
|
||||
char begin[8] = {0};
|
||||
char expected[] = {0x7F, 'E', 'L', 'F', 1, 1, 1, 0};
|
||||
fread(begin, 1, sizeof(begin), decrypted_license);
|
||||
if(memcmp(begin, expected, sizeof(begin)) != 0) {
|
||||
// This happens when decryption fails
|
||||
printf("You are not authorized to use this software!\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
const int maxninput = 255;
|
||||
printf("Please enter the flag: ");
|
||||
fflush(stdout);
|
||||
@@ -34,9 +94,9 @@ int main(){
|
||||
memcpy(memory + SECTION_USERDATA + 1, userinput, real_len);
|
||||
|
||||
// Load elf, keep 512 bytes as safe buffer
|
||||
int load_status = load_elf_object_sections(EMU_PROGRAM_PATH, memory, cpu.mem_size - SIZE_USERDATA);
|
||||
int load_status = load_elf_object_sections(decrypted_license, memory, cpu.mem_size - SIZE_USERDATA);
|
||||
if (load_status != LOAD_OK) {
|
||||
fprintf(stderr, "ELF load failed with code %d for '%s'\n", load_status, EMU_PROGRAM_PATH);
|
||||
fprintf(stderr, "ELF load failed with code %d for decrypted license\n", load_status);
|
||||
fflush(stderr);
|
||||
printf("ELF load failed with code %d\n", load_status);
|
||||
fflush(stdout);
|
||||
@@ -45,7 +105,7 @@ int main(){
|
||||
|
||||
uint32_t entry_offset = 0u;
|
||||
int symbol_status = resolve_elf_symbol_memory_offset(
|
||||
EMU_PROGRAM_PATH, EMU_ENTRY_SYMBOL, cpu.mem_size, &entry_offset);
|
||||
decrypted_license, EMU_ENTRY_SYMBOL, cpu.mem_size, &entry_offset);
|
||||
if (symbol_status != LOAD_OK) {
|
||||
fprintf(stderr, "Entry symbol '%s' resolve failed with code %d for '%s'\n",
|
||||
EMU_ENTRY_SYMBOL, symbol_status, EMU_PROGRAM_PATH);
|
||||
@@ -72,9 +132,9 @@ int main(){
|
||||
uint32_t result = (uint32_t)cpu.x[10];
|
||||
|
||||
if(result == 1) {
|
||||
printf("Congrats! That's the flag!");
|
||||
printf("Congrats! That's the flag!\n");
|
||||
} else {
|
||||
printf("Nope, sorry, that's incorrect!");
|
||||
printf("Nope, sorry, that's incorrect!\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user