Skip to content
Snippets Groups Projects
Commit 30fb1284 authored by Yuxiao Mao's avatar Yuxiao Mao
Browse files

build : separate attack and victim code from main

parent 8f63ae33
No related branches found
No related tags found
No related merge requests found
......@@ -6,8 +6,8 @@ OBJCOPY = $(CROSS_COMPILER)objcopy
OBJDUMP = $(CROSS_COMPILER)objdump
SRCDIR = src/
MAINFILES = $(SRCDIR)crt.S $(SRCDIR)main.c $(SRCDIR)lib/myorca_uart.c $(SRCDIR)lib/myorca_utils.c $(SRCDIR)lib/orca_malloc.c $(SRCDIR)lib-attack/fr.c $(SRCDIR)lib-attack/timestats.c $(SRCDIR)lib-attack/vlist.c $(SRCDIR)lib-crypto/aes.c
MAINOBJ = crt.o main.o myorca_utils.o myorca_uart.o orca_malloc.o fr.o timestats.o vlist.o aes.o
MAINFILES = $(SRCDIR)crt.S $(SRCDIR)main.c $(SRCDIR)attack.c $(SRCDIR)victim.c $(SRCDIR)lib/myorca_uart.c $(SRCDIR)lib/myorca_utils.c $(SRCDIR)lib/orca_malloc.c $(SRCDIR)lib-attack/fr.c $(SRCDIR)lib-attack/timestats.c $(SRCDIR)lib-attack/vlist.c $(SRCDIR)lib-crypto/aes.c
MAINOBJ = crt.o main.o attack.o victim.o myorca_utils.o myorca_uart.o orca_malloc.o fr.o timestats.o vlist.o aes.o
CCFLAGS = -I$(SRCDIR)lib -I$(SRCDIR)lib-attack -I$(SRCDIR)lib-crypto -I$(CROSS_COMPILER_LIB_PATH)
......
#include "attack.h"
static fr_t attack_fr;
void attack_prepare(void * monitor_addr) {
attack_fr = fr_prepare();
fr_monitor(attack_fr, monitor_addr);
}
//res=2 if cache hit, res=20 if cache miss
void attack_1(uint16_t *res) {
fr_probe(attack_fr, res);
}
#ifndef __ATTACK_H__
#define __ATTACK_H__
#include <stdint.h>
#include "fr.h"
void attack_prepare(void * monitor_addr);
void attack_1(uint16_t *res);
#endif //#ifndef __ATTACK_H__
......@@ -2,33 +2,24 @@
#include "orca_malloc.h"
#include "myorca_uart.h"
// Attack
// Configure
#define __OPTION_ATTACK_ENABLE__ 1
#include "fr.h"
// Victim
#define __OPTION_VICTIM_ENABLE__ 1
#define CBC 1
#define CTR 0
#define ECB 0
#define AES128 1
#define MULTIPLY_AS_A_FUNCTION 1
#include "aes.h"
#include "myorca_utils.h"
// Attack and Victim code
#if __OPTION_ATTACK_ENABLE__ == 1
#include "attack.h"
#endif
#if __OPTION_VICTIM_ENABLE__ == 1
#include "victim.h"
#endif
// ----------------------------------------------------------------------------
// Function declarations
// ----------------------------------------------------------------------------
int handle_exception();
#if __OPTION_VICTIM_ENABLE__ == 1
void victim_prepare(struct AES_ctx* ctx, const uint8_t* key, const uint8_t* iv, uint8_t* in);
#endif
void victim_1(int* val_addr);
#if __OPTION_ATTACK_ENABLE__ == 1
fr_t attack_prepare();
void attack_1(fr_t fr, uint16_t *res);
#endif
// ----------------------------------------------------------------------------
......@@ -38,19 +29,6 @@ void attack_1(fr_t fr, uint16_t *res);
char myorca_heap[HEAP_SIZE_BYTE];
int victim_data_address[16];
#if __OPTION_VICTIM_ENABLE__ == 1
const uint8_t c_key[] = { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c };
const uint8_t c_out[] = { 0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46, 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,
0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee, 0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2,
0x73, 0xbe, 0xd6, 0xb8, 0xe3, 0xc1, 0x74, 0x3b, 0x71, 0x16, 0xe6, 0x9e, 0x22, 0x22, 0x95, 0x16,
0x3f, 0xf1, 0xca, 0xa1, 0x68, 0x1f, 0xac, 0x09, 0x12, 0x0e, 0xca, 0x30, 0x75, 0x86, 0xe1, 0xa7 };
const uint8_t c_iv[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
const uint8_t c_in[] = { 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 };
#endif //if __OPTION_VICTIM_ENABLE__ == 1
// ----------------------------------------------------------------------------
// Main
......@@ -62,24 +40,15 @@ int main(void)
#if __OPTION_ATTACK_ENABLE__ == 1
uint16_t res[1];
fr_t fr = attack_prepare(victim_data_address);
attack_prepare(victim_data_address);
uart_printf("\r\nATTACK_ENABLE");
#endif //if __OPTION_ATTACK_ENABLE__ == 1
#endif
#if __OPTION_VICTIM_ENABLE__ == 1
struct AES_ctx ctx;
uint8_t in[64];
orca_memcpy(in, c_in, 64);
victim_prepare(&ctx, c_key, c_iv, in);
if (0 == orca_memcmp((char*) c_out, (char*) in, 64)) {
uart_printf("\r\nSUCCESS!");
} else {
uart_printf("\r\nFAILURE!");
}
uart_printf("\r\nVICTIM_ENABLE");
#endif //if __OPTION_VICTIM_ENABLE__ == 1
test_aes_ctx();
#endif
unsigned int uart_read_fifo_count = 0;
while (1) {
wait(1000);
// UART read -> Interrupt?
......@@ -94,7 +63,10 @@ int main(void)
victim_1(victim_data_address);
uart_printf("--");
}
attack_1(fr, res);
attack_1(res);
if (res[0] < 3) {
uart_printc('A');
}
#else
}
#endif
......@@ -117,40 +89,11 @@ int handle_exception() {
// ----------------------------------------------------------------------------
// Victim code
// victim_1 provide simple memory access if victim is disable
// victim_1 provide simple memory access
// ----------------------------------------------------------------------------
#if __OPTION_VICTIM_ENABLE__ == 1
void victim_prepare(struct AES_ctx* ctx, const uint8_t* key, const uint8_t* iv, uint8_t* in) {
AES_init_ctx_iv(ctx, key, iv);
AES_CBC_encrypt_buffer(ctx, in, 64);
}
#endif
void victim_1(int* val_addr) {
uart_printc('V');
uart_printi(val_addr[0]);
val_addr[0] = val_addr[0] + 1;
}
// ----------------------------------------------------------------------------
// Attack code
// ----------------------------------------------------------------------------
#if __OPTION_ATTACK_ENABLE__ == 1
fr_t attack_prepare(void * monitor_addr) {
fr_t fr = fr_prepare();
fr_monitor(fr, monitor_addr);
return fr;
}
//res=2 if cache hit, res=20 if cache miss
void attack_1(fr_t fr, uint16_t *res) {
fr_probe(fr, res);
if (res[0] < 3) {
uart_printc('A');
}
}
#endif //if __OPTION_ATTACK_ENABLE__ == 1
#include "orca_malloc.h"
#include "myorca_uart.h"
#include "myorca_utils.h"
#include "victim.h"
const uint8_t c_key[] = { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c };
const uint8_t c_out[] = { 0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46, 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,
0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee, 0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2,
0x73, 0xbe, 0xd6, 0xb8, 0xe3, 0xc1, 0x74, 0x3b, 0x71, 0x16, 0xe6, 0x9e, 0x22, 0x22, 0x95, 0x16,
0x3f, 0xf1, 0xca, 0xa1, 0x68, 0x1f, 0xac, 0x09, 0x12, 0x0e, 0xca, 0x30, 0x75, 0x86, 0xe1, 0xa7 };
const uint8_t c_iv[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
const uint8_t c_in[] = { 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 };
void victim_prepare(struct AES_ctx* ctx, const uint8_t* key, const uint8_t* iv, uint8_t* in) {
AES_init_ctx_iv(ctx, key, iv);
AES_CBC_encrypt_buffer(ctx, in, 64);
}
int test_aes_ctx() {
struct AES_ctx ctx;
uint8_t in[64];
orca_memcpy(in, c_in, 64);
victim_prepare(&ctx, c_key, c_iv, in);
if (0 == orca_memcmp((char*) c_out, (char*) in, 64)) {
uart_printf("\r\nAES128 CTX SUCCESS!");
return 0;
} else {
uart_printf("\r\nAES128 CTX FAILURE!");
return 1;
}
}
#ifndef __VICTIM_H__
#define __VICTIM_H__
#define CBC 1
#define CTR 0
#define ECB 0
#define AES128 1
#define MULTIPLY_AS_A_FUNCTION 1
#include "aes.h"
int test_aes_ctx();
#endif //#ifndef __VICTIM_H__
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment