lots of changes, some project restructuring and a new experiment
This commit is contained in:
@@ -6,14 +6,16 @@ if(${CMAKE_HOST_SYSTEM_PROCESSOR} STREQUAL "x86_64")
|
||||
set(CMAKE_C_COMPILER /usr/bin/aarch64-linux-gnu-gcc)
|
||||
set(CMAKE_ASM_COMPILER /usr/bin/aarch64-linux-gnu-as)
|
||||
set(CMAKE_OBJCOPY /usr/bin/aarch64-linux-gnu-objcopy)
|
||||
set(CMAKE_RANLIB /usr/bin/aarch64-linux-gnu-ranlib)
|
||||
elseif(${CMAKE_HOST_SYSTEM_PROCESSOR} STREQUAL "armv7l")
|
||||
# raspberry pi
|
||||
set(CMAKE_C_COMPILER /opt/cross/bin/aarch64-linux-gcc)
|
||||
set(CMAKE_ASM_COMPILER /opt/cross/bin/aarch64-linux-as)
|
||||
set(CMAKE_OBJCOPY /opt/cross/bin/aarch64-linux-objcopy)
|
||||
set(CMAKE_RANLIB /opt/cross/bin/aarch64-linux-ranlib)
|
||||
endif()
|
||||
|
||||
set(CMAKE_C_FLAGS "-nostdlib -O -Wl,--gc-sections")
|
||||
set(CMAKE_C_FLAGS "-nostdlib -O")
|
||||
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/bin)
|
||||
|
||||
foreach(NAME ${PL_NAMES})
|
||||
@@ -34,4 +36,9 @@ foreach(NAME ${PL_NAMES})
|
||||
ARGS -O binary -j .text -j .payload_text
|
||||
${CMAKE_CURRENT_BINARY_DIR}/payload_${NAME}
|
||||
${CMAKE_CURRENT_BINARY_DIR}/bin/payload_${NAME}.bin)
|
||||
endforeach(NAME)
|
||||
endforeach(NAME)
|
||||
|
||||
add_library(crypto_dev ../crypto/aes_sw_impl.c)
|
||||
|
||||
target_link_libraries(payload_aes_sw_bern crypto_dev)
|
||||
target_link_libraries(payload_aes_sw_corr crypto_dev)
|
||||
13
c8_remote/lib/payload/include/crypto_dev.h
Normal file
13
c8_remote/lib/payload/include/crypto_dev.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#ifndef CHECKM8_TOOL_CRYPTO_H
|
||||
#define CHECKM8_TOOL_CRYPTO_H
|
||||
|
||||
#include "bootrom_type.h"
|
||||
|
||||
void expand_key(unsigned char key[16], unsigned char key_sched[176],
|
||||
int n, struct aes_constants *c);
|
||||
|
||||
void aes128_encrypt_ecb(unsigned char *msg, unsigned int msg_len,
|
||||
unsigned char key_sched[176], struct aes_constants *c);
|
||||
|
||||
|
||||
#endif //CHECKM8_TOOL_CRYPTO_H
|
||||
@@ -1,218 +0,0 @@
|
||||
#include "bootrom_func.h"
|
||||
#include "bootrom_type.h"
|
||||
#include "cacheutil.h"
|
||||
|
||||
PAYLOAD_SECTION
|
||||
void sub_bytes(unsigned char block[16], struct aes_constants *c)
|
||||
{
|
||||
int i;
|
||||
unsigned char val;
|
||||
|
||||
for(i = 0; i < 16; i++)
|
||||
{
|
||||
val = block[i];
|
||||
block[i] = c->sbox[val >> 4u][val & 0xfu];
|
||||
}
|
||||
}
|
||||
|
||||
PAYLOAD_SECTION
|
||||
void shift_rows(unsigned char block[16])
|
||||
{
|
||||
unsigned char temp1, temp2;
|
||||
|
||||
temp1 = block[0x1];
|
||||
block[0x1] = block[0x5];
|
||||
block[0x5] = block[0x9];
|
||||
block[0x9] = block[0xd];
|
||||
block[0xd] = temp1;
|
||||
|
||||
temp1 = block[0x2];
|
||||
temp2 = block[0xe];
|
||||
block[0x2] = block[0xa];
|
||||
block[0xe] = block[0x6];
|
||||
block[0xa] = temp1;
|
||||
block[0x6] = temp2;
|
||||
|
||||
temp1 = block[0x3];
|
||||
block[0x3] = block[0xf];
|
||||
block[0xf] = block[0xb];
|
||||
block[0xb] = block[0x7];
|
||||
block[0x7] = temp1;
|
||||
}
|
||||
|
||||
PAYLOAD_SECTION
|
||||
void mix_cols(unsigned char block[16], struct aes_constants *c)
|
||||
{
|
||||
unsigned char r0, r1, r2, r3;
|
||||
int i;
|
||||
|
||||
for(i = 0; i < 4; i++)
|
||||
{
|
||||
r0 = block[4 * i];
|
||||
r1 = block[4 * i + 1];
|
||||
r2 = block[4 * i + 2];
|
||||
r3 = block[4 * i + 3];
|
||||
|
||||
// no reason for the "+ 0" here but it makes the code look more lined up :)
|
||||
block[4 * i + 0] = c->mul2[r0] ^ c->mul3[r1] ^ r2 ^ r3;
|
||||
block[4 * i + 1] = r0 ^ c->mul2[r1] ^ c->mul3[r2] ^ r3;
|
||||
block[4 * i + 2] = r0 ^ r1 ^ c->mul2[r2] ^ c->mul3[r3];
|
||||
block[4 * i + 3] = c->mul3[r0] ^ r1 ^ r2 ^ c->mul2[r3];
|
||||
}
|
||||
}
|
||||
|
||||
PAYLOAD_SECTION
|
||||
void add_key(unsigned char block[16], unsigned char key[16])
|
||||
{
|
||||
int i;
|
||||
for(i = 0; i < 16; i++)
|
||||
{
|
||||
block[i] = block[i] ^ key[i];
|
||||
}
|
||||
}
|
||||
|
||||
PAYLOAD_SECTION
|
||||
void expand_key(unsigned char key[16], unsigned char key_sched[176], int n,
|
||||
struct aes_constants *c)
|
||||
{
|
||||
int i, j, prev_key_base, key_base = 0;
|
||||
unsigned char val;
|
||||
for(i = 0; i < 16; i++)
|
||||
{
|
||||
key_sched[i] = key[i];
|
||||
}
|
||||
|
||||
for(i = 1; i < n; i++)
|
||||
{
|
||||
prev_key_base = key_base;
|
||||
key_base = 16 * i;
|
||||
|
||||
for(j = 0; j < 3; j++)
|
||||
{
|
||||
val = key_sched[prev_key_base + 13 + j];
|
||||
key_sched[key_base + j] = c->sbox[val >> 4u][val & 0xfu];
|
||||
}
|
||||
|
||||
val = key_sched[prev_key_base + 12];
|
||||
key_sched[key_base + 3] = c->sbox[val >> 4u][val & 0xfu];
|
||||
|
||||
key_sched[key_base] ^= c->rc_lookup[i - 1];
|
||||
|
||||
for(j = 0; j < 4; j++)
|
||||
{
|
||||
key_sched[key_base + j] = key_sched[key_base + j] ^ key_sched[prev_key_base + j];
|
||||
}
|
||||
|
||||
for(j = 4; j < 16; j++)
|
||||
{
|
||||
key_sched[key_base + j] = key_sched[key_base + j - 4] ^ key_sched[prev_key_base + j];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PAYLOAD_SECTION
|
||||
void aes128_encrypt_ecb(unsigned char *msg, unsigned int msg_len,
|
||||
unsigned char key_sched[176], struct aes_constants *c)
|
||||
{
|
||||
unsigned int num_blocks = msg_len / 16;
|
||||
unsigned char *block;
|
||||
|
||||
unsigned int i, j;
|
||||
for(i = 0; i < num_blocks; i++)
|
||||
{
|
||||
block = &msg[16 * i];
|
||||
add_key(block, key_sched);
|
||||
|
||||
for(j = 0; j < 9; j++)
|
||||
{
|
||||
sub_bytes(block, c);
|
||||
shift_rows(block);
|
||||
mix_cols(block, c);
|
||||
add_key(block, &key_sched[16 * (j + 1)]);
|
||||
}
|
||||
|
||||
sub_bytes(block, c);
|
||||
shift_rows(block);
|
||||
add_key(block, &key_sched[16 * (j + 1)]);
|
||||
}
|
||||
}
|
||||
|
||||
PAYLOAD_SECTION
|
||||
uint64_t entry_sync(unsigned char *msg, unsigned int msg_len, unsigned char key[16],
|
||||
struct aes_constants *c)
|
||||
{
|
||||
unsigned long long start = 0;
|
||||
unsigned char key_sched[176];
|
||||
expand_key(key, key_sched, 11, c);
|
||||
|
||||
start = get_ticks();
|
||||
aes128_encrypt_ecb(msg, msg_len, key, c);
|
||||
return get_ticks() - start;
|
||||
}
|
||||
|
||||
PAYLOAD_SECTION
|
||||
void entry_async(uint64_t *base)
|
||||
{
|
||||
int i, j, iter_count = 0;
|
||||
unsigned long long start = 0;
|
||||
struct event *usb_event = ADDR_USB_EVENT;
|
||||
|
||||
unsigned char msg_old[16];
|
||||
unsigned char key_sched[176];
|
||||
double timing;
|
||||
|
||||
// get initial params
|
||||
unsigned char *msg = (unsigned char *) base[0];
|
||||
unsigned int msg_len = (unsigned int) base[1];
|
||||
unsigned char *key = (unsigned char *) base[2];
|
||||
struct aes_constants *c = (struct aes_constants *) base[3];
|
||||
|
||||
expand_key(key, key_sched, 11, c);
|
||||
|
||||
// initialize events and buffers
|
||||
struct bern_data *data = (struct bern_data *) base;
|
||||
event_new(&data->ev_data, 1, 0);
|
||||
event_new(&data->ev_done, 1, 0);
|
||||
|
||||
data->count = 0;
|
||||
for(i = 0; i < 16; i++)
|
||||
{
|
||||
for(j = 0; j < 256; j++)
|
||||
{
|
||||
data->t[i][j] = 0;
|
||||
data->tsq[i][j] = 0;
|
||||
data->tnum[i][j] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
while(1)
|
||||
{
|
||||
// randomly generate a new msg based on the old one
|
||||
for(i = 0; i < 16; i++)
|
||||
msg_old[i] = msg[i];
|
||||
|
||||
// encrypt it and measure time
|
||||
start = get_ticks();
|
||||
aes128_encrypt_ecb(msg, msg_len, key_sched, c);
|
||||
timing = (double) (get_ticks() - start);
|
||||
|
||||
// update counters
|
||||
for(i = 0; i < 16; i++)
|
||||
{
|
||||
data->t[i][msg_old[i]] += timing;
|
||||
data->tsq[i][msg_old[i]] += (timing * timing);
|
||||
data->tnum[i][msg_old[i]] += 1;
|
||||
|
||||
data->count++;
|
||||
data->ttotal += timing;
|
||||
}
|
||||
|
||||
// check if host has requested data
|
||||
iter_count++;
|
||||
if(iter_count % 100000 == 0)
|
||||
{
|
||||
if(event_try(&data->ev_data, 1))
|
||||
event_wait(&data->ev_done);
|
||||
}
|
||||
}
|
||||
}
|
||||
83
c8_remote/lib/payload/src/aes_sw_bern.c
Normal file
83
c8_remote/lib/payload/src/aes_sw_bern.c
Normal file
@@ -0,0 +1,83 @@
|
||||
#include "bootrom_func.h"
|
||||
#include "bootrom_type.h"
|
||||
#include "cacheutil.h"
|
||||
#include "crypto_dev.h"
|
||||
|
||||
PAYLOAD_SECTION
|
||||
uint64_t entry_sync(unsigned char *msg, unsigned int msg_len, unsigned char key[16],
|
||||
struct aes_constants *c)
|
||||
{
|
||||
unsigned long long start = 0;
|
||||
unsigned char key_sched[176];
|
||||
expand_key(key, key_sched, 11, c);
|
||||
|
||||
start = get_ticks();
|
||||
aes128_encrypt_ecb(msg, msg_len, key, c);
|
||||
return get_ticks() - start;
|
||||
}
|
||||
|
||||
PAYLOAD_SECTION
|
||||
void entry_async(uint64_t *base)
|
||||
{
|
||||
int i, j, iter_count = 0;
|
||||
unsigned long long start = 0;
|
||||
|
||||
unsigned char msg_old[16];
|
||||
unsigned char key_sched[176];
|
||||
double timing;
|
||||
|
||||
// get initial params
|
||||
unsigned char *msg = (unsigned char *) base[0];
|
||||
unsigned int msg_len = (unsigned int) base[1];
|
||||
unsigned char *key = (unsigned char *) base[2];
|
||||
struct aes_constants *c = (struct aes_constants *) base[3];
|
||||
|
||||
expand_key(key, key_sched, 11, c);
|
||||
|
||||
// initialize events and buffers
|
||||
struct bern_data *data = (struct bern_data *) base;
|
||||
event_new(&data->ev_data, 1, 0);
|
||||
event_new(&data->ev_done, 1, 0);
|
||||
|
||||
data->count = 0;
|
||||
for(i = 0; i < 16; i++)
|
||||
{
|
||||
for(j = 0; j < 256; j++)
|
||||
{
|
||||
data->t[i][j] = 0;
|
||||
data->tsq[i][j] = 0;
|
||||
data->tnum[i][j] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
while(1)
|
||||
{
|
||||
// randomly generate a new msg based on the old one
|
||||
for(i = 0; i < 16; i++)
|
||||
msg_old[i] = msg[i];
|
||||
|
||||
// encrypt it and measure time
|
||||
start = get_ticks();
|
||||
aes128_encrypt_ecb(msg, msg_len, key_sched, c);
|
||||
timing = (double) (get_ticks() - start);
|
||||
|
||||
// update counters
|
||||
for(i = 0; i < 16; i++)
|
||||
{
|
||||
data->t[i][msg_old[i]] += timing;
|
||||
data->tsq[i][msg_old[i]] += (timing * timing);
|
||||
data->tnum[i][msg_old[i]] += 1;
|
||||
|
||||
data->count++;
|
||||
data->ttotal += timing;
|
||||
}
|
||||
|
||||
// check if host has requested data
|
||||
iter_count++;
|
||||
if(iter_count % 100000 == 0)
|
||||
{
|
||||
if(event_try(&data->ev_data, 1))
|
||||
event_wait(&data->ev_done);
|
||||
}
|
||||
}
|
||||
}
|
||||
54
c8_remote/lib/payload/src/aes_sw_corr.c
Normal file
54
c8_remote/lib/payload/src/aes_sw_corr.c
Normal file
@@ -0,0 +1,54 @@
|
||||
#include "bootrom_func.h"
|
||||
#include "dev_util.h"
|
||||
#include "crypto_dev.h"
|
||||
|
||||
PAYLOAD_SECTION
|
||||
void entry_sync()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
PAYLOAD_SECTION
|
||||
void entry_async(uint64_t *base)
|
||||
{
|
||||
int i;
|
||||
unsigned char key_sched[176];
|
||||
unsigned long long start, timing;
|
||||
|
||||
unsigned char *key = (unsigned char *) base[0];
|
||||
struct aes_constants *c = (struct aes_constants *) base[1];
|
||||
|
||||
struct corr_data *data = (struct corr_data *) base;
|
||||
event_new(&data->ev_cont, 1, 0);
|
||||
|
||||
expand_key(key, key_sched, 11, c);
|
||||
for(i = 0; i < 16; i++)
|
||||
data->msg[i] = 0;
|
||||
|
||||
while(1)
|
||||
{
|
||||
// reset data state
|
||||
data->num_cutoff = 0;
|
||||
for(i = 0; i < N_CORR_ENTRIES; i++)
|
||||
{
|
||||
data->data[i] = 0;
|
||||
}
|
||||
|
||||
// collect new data
|
||||
i = 0;
|
||||
while(i < N_CORR_ENTRIES)
|
||||
{
|
||||
start = get_ticks();
|
||||
aes128_encrypt_ecb(data->msg, 16, key_sched, c);
|
||||
timing = get_ticks() - start;
|
||||
|
||||
if(timing < 256)
|
||||
data->data[i++] = (unsigned char) timing;
|
||||
else
|
||||
data->num_cutoff++;
|
||||
}
|
||||
|
||||
event_wait(&data->ev_cont);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user