Skip to content
Snippets Groups Projects
main.c 3.92 KiB
Newer Older
#include "orca_malloc.h"
#include "myorca_uart.h"
#define __OPTION_ATTACK_ENABLE__ 1
#define __OPTION_VICTIM_ENABLE__ 1

// Attack and Victim code
#if __OPTION_ATTACK_ENABLE__ == 1
#include "attack.h"
#endif
#if __OPTION_VICTIM_ENABLE__ == 1
#include "victim.h"
#endif

// ----------------------------------------------------------------------------
Yuxiao Mao's avatar
Yuxiao Mao committed
// Function declarations
// ----------------------------------------------------------------------------
Yuxiao Mao's avatar
Yuxiao Mao committed
int handle_exception();
// ----------------------------------------------------------------------------
// Global variable
// Place order seems to be the same :
// static before non static, used array before unused
// heap at the end if non static, preserve order if all static
// ----------------------------------------------------------------------------
#define HEAP_SIZE_BYTE 1024
unsigned char myorca_heap[HEAP_SIZE_BYTE];

// Cache orca : tag (23b), num_line (4b = 16line), pos (5b = 32byte)
// To cover all cache lines = 16lines * 4byte/int * 8int
// But if we monitor all cache lines, attack_fr structure and heap likely occupy
// all address and thought can't detect any eviction.
// In this configuration of 8 lines, we can detect 5-7 access.

// MONITOR_CACHE_LINE must < 16, else need change VLIST_DEF_SIZE, victim
// address selection, attack line detect print logic.
#define MONITOR_CACHE_LINE 8
#define INT_PER_CACHE_LINE 8
static int32_t victim_data_address[16*INT_PER_CACHE_LINE];

// ----------------------------------------------------------------------------
// Main
// ----------------------------------------------------------------------------
  // Prepare heap
  orca_init_malloc(myorca_heap, HEAP_SIZE_BYTE, 4);
#if __OPTION_ATTACK_ENABLE__ == 1
  attack_prepare(victim_data_address);
  for (int i = 1; i < MONITOR_CACHE_LINE; i++) {
    attack_add_monitor(&victim_data_address[i*INT_PER_CACHE_LINE]);
  }
  uart_printf("\r\nATTACK_ENABLE");
#if __OPTION_VICTIM_ENABLE__ == 1
  test_aes_ctx();
#endif
  unsigned int uart_read_fifo_count = 0;
Yuxiao Mao's avatar
Yuxiao Mao committed
    // UART read -> Interrupt?
    uart_read_fifo_count = uart_get_read_fifo_count();
    if (uart_read_fifo_count > 0) {
      char read_value = uart_read_char();
      uart_printf("\r\nReceived value from Uart: (");
      uart_printc(read_value);
      uart_printf(").");

#if __OPTION_ATTACK_ENABLE__ == 1
      int read_number = a2d(read_value);
      if (read_number >= 0 && read_number < MONITOR_CACHE_LINE) {
        victim_1(&victim_data_address[read_number*INT_PER_CACHE_LINE]);
      } else {
        victim_1(victim_data_address);
      }
      uart_printf("--");
    for (int i = 0; i < MONITOR_CACHE_LINE; i++) {
      if (res[i] < 3) {
        uart_printc('a');
        uart_printc(i+(i<10 ? '0' : 'A'-10));
      }

// ----------------------------------------------------------------------------
// Exception handling
// ----------------------------------------------------------------------------
Yuxiao Mao's avatar
Yuxiao Mao committed
int handle_exception() {
  uart_printf("Exception\r\n");
  while (1) {
  }
Yuxiao Mao's avatar
Yuxiao Mao committed
  return 0;
}
// ----------------------------------------------------------------------------
// Others functions
// ----------------------------------------------------------------------------
int a2d(char ch)
{
	if (ch>='0' && ch<='9')
		return ch-'0';
	else if (ch>='a' && ch<='f')
		return ch-'a'+10;
	else if (ch>='A' && ch<='F')
		return ch-'A'+10;
	else return -1;
}


// ----------------------------------------------------------------------------
// Victim code
// victim_1 provide simple memory access
// ----------------------------------------------------------------------------
  uart_printc('V');
  uart_printi(*val_addr); //take int
  *val_addr = *val_addr + 1;