make programming slightly more robust

pull/33/head
conor 2016-06-02 23:14:32 -04:00
rodzic cd173cb7a5
commit d8d6e3ca1f
8 zmienionych plików z 76 dodań i 140 usunięć

Wyświetl plik

@ -15,7 +15,7 @@
// application settings
#define U2F_ATTESTATION_KEY_SLOT 15
//#define ATECC_SETUP_DEVICE
//#define U2F_PRINT
#define U2F_PRINT
// efm8ub1 application eeprom memory mappings
#define U2F_KEY_HEADER_ADDR 0xF800
@ -24,12 +24,12 @@
// {blue(0), green(0x5a), red(0)}
#define U2F_DEFAULT_BRIGHTNESS 90
#define U2F_DEFAULT_COLOR 0x005a00
#define U2F_COLOR 0x005a00
#define U2F_DEFAULT_COLOR_PRIME 0x5a0000
#define U2F_DEFAULT_COLOR_ERROR 0x0000c8
#define U2F_DEFAULT_COLOR_INPUT 0x008080
#define U2F_DEFAULT_COLOR_INPUT_SUCCESS 0x809600
#define U2F_DEFAULT_COLOR_WINK 0x960000
#define U2F_COLOR_WINK 0x960000
#define U2F_DEFAULT_COLOR_WINK_OUT_OF_SPACE 0x0f0f96
#define U2F_DEFAULT_COLOR_PERIOD 20
@ -64,6 +64,8 @@ typedef enum
ERROR_HID_BUFFER_FULL = 0x17,
ERROR_HID_INVALID_CMD = 0x18,
ERROR_DAMN_WATCHDOG = 0x19,
ERROR_OUT_OF_CIDS = 0x20,
ERROR_I2C_RESTART = 0x21,
}
APP_ERROR_CODE;
@ -73,13 +75,6 @@ struct APP_DATA
uint8_t tmp[70];
};
struct APP_CONF
{
uint16_t pulse_period;
uint32_t idle_color;
uint32_t idle_color_prime;
};
#define U2F_CONFIG_GET_SERIAL_NUM 0x80
#define U2F_CONFIG_IS_BUILD 0x81
#define U2F_CONFIG_IS_CONFIGURED 0x82
@ -109,11 +104,9 @@ uint8_t get_app_state();
void set_app_state(APP_STATE s);
void flush_app_conf();
void rgb(uint8_t * c);
void rgb(uint8_t r, uint8_t g, uint8_t b);
void rgb_hex(uint32_t _rgb);
void app_wink(uint32_t c);
void app_wink(uint32_t color);
// should be called after initializing eeprom
void u2f_init();

Wyświetl plik

@ -133,7 +133,7 @@ int8_t atecc_send_recv(uint8_t cmd, uint8_t p1, uint16_t p2,
errors++;
if (errors > 5)
{
return -1;
return -2;
}
switch(get_app_error())
{

Wyświetl plik

@ -54,34 +54,7 @@ uint8_t custom_command(struct u2f_hid_msg * msg)
break;
case U2F_CUSTOM_IDLE_COLOR:
U2FHID_SET_LEN(msg, 1);
appconf.idle_color = *((uint32_t*)msg->pkt.init.payload);
flush_app_conf();
msg->pkt.init.payload[0] = 1;
usb_write((uint8_t*)msg, 64);
break;
case U2F_CUSTOM_IDLE_COLORP:
U2FHID_SET_LEN(msg, 1);
appconf.idle_color_prime = *((uint32_t*)msg->pkt.init.payload);
flush_app_conf();
msg->pkt.init.payload[0] = 1;
usb_write((uint8_t*)msg, 64);
break;
case U2F_CUSTOM_PULSE:
U2FHID_SET_LEN(msg, 1);
appconf.pulse_period = *((uint16_t*)msg->pkt.init.payload);
flush_app_conf();
msg->pkt.init.payload[0] = 1;
usb_write((uint8_t*)msg, 64);
break;
default:
return 0;
}

Wyświetl plik

@ -17,7 +17,6 @@
#include "tests.h"
data struct APP_DATA appdata;
data struct APP_CONF appconf;
uint8_t error;
uint8_t state;
@ -59,12 +58,6 @@ void set_app_state(APP_STATE s)
state = s;
}
void flush_app_conf()
{
eeprom_erase(U2F_APP_CONFIG);
eeprom_write(U2F_APP_CONFIG, (uint8_t* )&appconf, sizeof(struct APP_CONF));
}
void app_wink(uint32_t c)
{
winkc = c;
@ -83,33 +76,33 @@ static uint32_t current_color;
static uint8_t brightness = 90;
void rgb(uint8_t * c)
void rgb(uint8_t r, uint8_t g, uint8_t b)
{
if (c[0])
if (r)
{
PCA0CPM2 |= PCA0CPM2_PWM__ENABLED;
LED_R(c[0]);
LED_R(r);
}
else
{
PCA0CPM2 &= ~PCA0CPM2_PWM__ENABLED;
}
if (c[2])
if (b)
{
PCA0CPM0 |= PCA0CPM0_PWM__ENABLED;
LED_B(c[2]);
LED_B(b);
}
else
{
PCA0CPM0 &= ~PCA0CPM0_PWM__ENABLED;
}
if (c[1])
if (g)
{
PCA0CPM1 |= PCA0CPM1_PWM__ENABLED;
LED_G(c[1]);
LED_G(g);
}
else
{
@ -120,15 +113,7 @@ void rgb(uint8_t * c)
void rgb_hex(uint32_t c)
{
color[0] = c;
color[1] = c>>8;
color[2] = c>>16;
current_color = c;
rgb(color);
color_max = color;
if (*color_max < color[1]) color_max++;
if (*color_max < color[2]) color_max = color + 2;
brightness = *color_max;
rgb(c,c>>8,c>>16);
}
@ -139,7 +124,7 @@ int16_t main(void) {
uint16_t ms_heart;
uint16_t ms_wink;
uint16_t ms_grad;
uint8_t winks = 0, light;
uint8_t winks = 0, light, grad_dir = 0;
int8_t grad_inc = 0;
enter_DefaultMode_from_RESET();
@ -162,7 +147,7 @@ int16_t main(void) {
run_tests();
atecc_setup_init(appdata.tmp);
rgb_hex(appconf.idle_color);
rgb_hex(U2F_COLOR);
while (1) {
@ -172,7 +157,7 @@ int16_t main(void) {
if (ms_since(ms_heart,500))
{
u2f_printl("ms ", 1, get_ms());
u2f_printd("ms ", 1, (uint16_t)get_ms());
}
if (!USBD_EpIsBusy(EP1OUT) && !USBD_EpIsBusy(EP1IN) && state != APP_HID_MSG)
@ -183,38 +168,26 @@ int16_t main(void) {
switch(state)
{
case APP_NOTHING:
if (ms_since(ms_grad, appconf.pulse_period))
if (ms_since(ms_grad, 10))
{
if (U2F_BUTTON_IS_PRESSED())
if (light == 90)
{
if (appconf.idle_color_prime != current_color)
{
rgb_hex(appconf.idle_color_prime);
}
grad_dir = 0;
}
else if (light == 0)
{
grad_dir = 1;
}
if (grad_dir)
if (U2F_BUTTON_IS_PRESSED())
rgb(0,0,light++);
else
rgb(0,light++,0);
else
{
if (appconf.idle_color != current_color)
{
rgb_hex(appconf.idle_color);
}
}
if (*color_max >= brightness)
{
grad_inc = -1;
}
else if (*color_max == 0)
{
grad_inc = 1;
}
if (brightness != 0)
{
*color_max += grad_inc;
}
rgb(color);
if (U2F_BUTTON_IS_PRESSED())
rgb(0,0,light--);
else
rgb(0,light--,0);
}
break;
case APP_HID_MSG:
@ -262,7 +235,7 @@ int16_t main(void) {
if (error)
{
u2f_printb("error: ", 1, error);
u2f_printx("error: ", 1, (uint16_t)error);
error = 0;
rgb_hex(U2F_DEFAULT_COLOR_ERROR);
while(!ms_since(ms_heart,2000))

Wyświetl plik

@ -82,21 +82,16 @@ int8_t u2f_wipe_keys()
void u2f_init()
{
uint8_t i,ec;
int8_t i,ec;
struct atecc_response res;
eeprom_read(U2F_APP_CONFIG, (uint8_t* )&appconf, sizeof(struct APP_CONF));
eeprom_read(U2F_EEPROM_CONFIG, (uint8_t* )&key_store, sizeof(struct key_storage_header));
// initialize key handles
if (key_store.num_keys != U2F_NUM_KEYS)
{
watchdog();
key_store.num_keys = U2F_NUM_KEYS;
key_store.valid_keys = 0;
key_store.num_issued = 0;
flush_key_store();
for (i=0; i < U2F_NUM_KEYS; i++)
{
@ -107,23 +102,23 @@ void u2f_init()
sizeof(appdata.tmp), &res);
if (ec != 0)
{
u2f_printb("REDO! REDO! ",1,i);
u2f_printb("REDO! REDO! ",2,i,-ec);
eeprom_erase(U2F_EEPROM_CONFIG);
// reset
reboot();
}
res.buf[0] = i+1;
appconf.pulse_period = 20;
appconf.idle_color = U2F_DEFAULT_COLOR;
appconf.idle_color_prime = U2F_DEFAULT_COLOR_PRIME;
flush_app_conf();
eeprom_write(U2F_KEYS_ADDR + i * U2F_KEY_HANDLE_SIZE,
res.buf, U2F_KEY_HANDLE_SIZE);
}
key_store.num_keys = U2F_NUM_KEYS;
key_store.valid_keys = 0;
key_store.num_issued = 0;
flush_key_store();
}
}

Wyświetl plik

@ -257,20 +257,17 @@ static void hid_u2f_parse(struct u2f_hid_msg* req)
switch(hid_layer.current_cmd)
{
case U2FHID_INIT:
//u2f_printlx("got init packet ",1,req->cid);
if (U2FHID_LEN(req) != 8)
{
// this one is safe
stamp_error(hid_layer.current_cid, ERR_INVALID_LEN);
u2f_prints("invalid len init\r\n");
goto fail;
}
u2f_hid_set_len(17);
//u2f_printlx("cid: ",1,hid_layer.current_cid);
if (hid_layer.current_cid == 0)
{
u2f_prints("out of cid's\r\n");
set_app_error(ERROR_OUT_OF_CIDS);
goto fail;
}
@ -346,7 +343,7 @@ static void hid_u2f_parse(struct u2f_hid_msg* req)
u2f_hid_set_len(0);
u2f_hid_writeback(NULL, 0);
u2f_hid_flush();
app_wink(U2F_DEFAULT_COLOR_WINK);
app_wink(U2F_COLOR_WINK);
break;
case U2FHID_LOCK:

Wyświetl plik

@ -14,45 +14,33 @@ fi
export PATH=$PATH:gencert:u2f_zero_client:flashing
# setup atecc
date +"%T"
echo "erasing..."
erase.sh
if [[ "$?" -ne "0" ]] ; then
while [[ "$?" -ne "0" ]] ; do
sleep .1
erase.sh
fi
done
[[ "$?" -ne "0" ]] && exit 1
date +"%T"
echo "programming setup..."
program.sh $SETUP_HEX
[[ "$?" -ne "0" ]] && exit 1
date +"%T"
echo "configuring..."
client.py configure pubkey.hex >/dev/null
if [[ "$?" -ne "0" ]] ; then
while [[ "$?" -ne "0" ]] ; do
sleep .2
client.py configure pubkey.hex >/dev/null
fi
done
if [[ "$?" -ne "0" ]] ; then
sleep .2
client.py configure pubkey.hex
fi
[[ "$?" -ne "0" ]] && exit 1
date +"%T"
echo "generate attestation certificate..."
gencert.sh "$1" "$(cat pubkey.hex)" attest.der > ../firmware/src/cert.c
[[ "$?" -ne "0" ]] && exit 1
date +"%T"
echo "building..."
PATH1=$PATH
@ -62,14 +50,17 @@ export PATH=$PATH1
[[ "$?" -ne "0" ]] && exit 1
date +"%T"
echo "programming final build..."
cp $FINAL_HEX prog.hex
program.sh prog.hex
#rm prog.hex
while [[ "$?" -ne "0" ]] ; do
sleep .2
program.sh prog.hex
done
[[ "$?" -ne "0" ]] && exit 1
date +"%T"
echo "done."

Wyświetl plik

@ -53,9 +53,12 @@ def open_u2f():
try:
h.open(0x10c4,0x8acf)
except IOError as ex:
print( ex)
print( 'U2F Zero not found')
sys.exit(1)
try:
h.open(0x10c4,0x8acf)
except:
print( ex)
print( 'U2F Zero not found')
sys.exit(1)
return h
def die(msg):
@ -90,6 +93,16 @@ def get_crc(data):
crc1 = (crc>>8) & 0xff;
return [crc1,crc2]
def read_n_tries(dev,tries,num,wait):
data = None
for i in range(0,tries-1):
try:
return dev.read(num,wait)
except:
time.sleep(.1)
pass
return dev.read(num,wait)
def do_configure(h,output):
config = "\x01\x23\x6d\x10\x00\x00\x50\x00\xd7\x2c\xa5\x71\xee\xc0\x85\x00\xc0\x00\x55\x00\x83\xa0\x83\xa0\x83\xa0\x83\xa0\x83\xa0\x83\xa0\x83\xa0\x83\xa0\x83\xa0\x83\xa0\x83\xa0\x83\xa0\x83\xa0\x83\xa0\x83\xa0\x83\xa0\xff\xff\xff\xff\x00\x00\x00\x00\xff\xff\xff\xff\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00\x00\x55\x55\xff\xff\x00\x00\x00\x00\x00\x00\x13\x00\x13\x00\x13\x00\x13\x00\x13\x00\x13\x00\x13\x00\x13\x00\x13\x00\x13\x00\x13\x00\x13\x00\x13\x00\x13\x00\x13\x00\x33\x00";
@ -106,7 +119,7 @@ def do_configure(h,output):
h.write([0,commands.U2F_CONFIG_GET_SERIAL_NUM])
while True:
data = h.read(64,1000)
data = read_n_tries(h,5,64,1000)
l = data[1]
print( 'read %i bytes' % l)
if data[0] == commands.U2F_CONFIG_GET_SERIAL_NUM:
@ -120,7 +133,8 @@ def do_configure(h,output):
crc = get_crc(config)
print( 'crc is ', [hex(x) for x in crc])
h.write([0,commands.U2F_CONFIG_LOCK] + crc)
data = h.read(64,1000)
data = read_n_tries(h,5,64,1000)
if data[1] == 1:
print( 'locked eeprom with crc ',crc)
else:
@ -129,7 +143,7 @@ def do_configure(h,output):
time.sleep(0.250)
h.write([0,commands.U2F_CONFIG_GENKEY])
data = h.read(64,1000)
data = read_n_tries(h,5,64,1000)
data = array.array('B',data).tostring()
data = binascii.hexlify(data)
print( 'generated key:')