clean up and added hid TODO's

vtcsec_summit
Conor 2016-03-30 21:48:01 -04:00
rodzic 3051bdda54
commit 6d0efa1a17
7 zmienionych plików z 75 dodań i 43 usunięć

Wyświetl plik

@ -52,6 +52,7 @@ typedef enum
ERROR_I2C_BAD_LEN = 0x16,
ERROR_HID_BUFFER_FULL = 0x17,
ERROR_HID_INVALID_CMD = 0x18,
ERROR_DAMN_WATCHDOG = 0x19,
}
APP_ERROR_CODE;
@ -104,13 +105,14 @@ void u2f_config_request();
#define U2F_DISABLE
#define u2f_init(x)
#define u2f_hid_init(x)
#define u2f_hid_request(x)
#define u2f_hid_request(x) atecc_setup_device((struct config_msg*)x)
#define u2f_hid_set_len(x)
#define u2f_hid_flush(x)
#define u2f_hid_writeback(x)
#else
#define atecc_setup_device(x)
#define atecc_setup_init(x)
#endif

Wyświetl plik

@ -54,7 +54,6 @@ extern data volatile uint8_t SMB_FLAGS;
#define SMB_BUSY 0x2
#define SMB_WRITE_EXT 0x4
#define SMB_READ_TRUNC 0x10
#define SMB_COMPUTE_CRC 0x20
#define SMB_RECV_NACK 0x40
#define SMB_READING() ((SMB_FLAGS & SMB_READ))
@ -62,8 +61,6 @@ extern data volatile uint8_t SMB_FLAGS;
#define SMB_WRITING_EXT() ((SMB_FLAGS & SMB_WRITE_EXT))
#define SMB_IS_BUSY() ((SMB_FLAGS & SMB_BUSY))
#define SMB_BUSY_CLEAR() (SMB_FLAGS &= ~SMB_BUSY)
#define SMB_HAS_CRC() (SMB_FLAGS & SMB_COMPUTE_CRC)
#define SMB_CRC_CLEAR() (SMB_FLAGS &= ~SMB_COMPUTE_CRC)
#define SMB_WAS_NACKED() (SMB_FLAGS & SMB_RECV_NACK)
void smb_init();
@ -78,8 +75,6 @@ void smb_write (uint8_t addr, uint8_t* buf, uint8_t len);
void smb_set_ext_write( uint8_t* extbuf, uint8_t extlen);
void smb_init_crc();
// reverse bits for a 16 bit int
uint16_t reverse_bits(uint16_t crc);

Wyświetl plik

@ -129,7 +129,6 @@ SI_INTERRUPT (SMBUS0_ISR, SMBUS0_IRQn)
SMB0DAT = (uint8_t)(SMB_crc>>8);
break;
case 2:
SMB_CRC_CLEAR();
SMB0CN0_STO = 1;
SMB_BUSY_CLEAR();
}
@ -163,10 +162,8 @@ SI_INTERRUPT (SMBUS0_ISR, SMBUS0_IRQn)
else
{
// end transaction
if (SMB_HAS_CRC())
{
SMB_crc = reverse_bits(SMB_crc);
}
SMB_crc = reverse_bits(SMB_crc);
SMB_BUSY_CLEAR();
SMB0CN0_ACK = 0;
SMB0CN0_STO = 1;

Wyświetl plik

@ -26,7 +26,6 @@ int8_t atecc_send(uint8_t cmd, uint8_t p1, uint16_t p2,
params[4] = ((uint8_t*)&p2)[1];
params[5] = ((uint8_t* )&p2)[0];
smb_init_crc();
smb_set_ext_write(buf, len);
smb_write( ATECC508A_ADDR, params, sizeof(params));
if (SMB_WAS_NACKED())
@ -56,7 +55,6 @@ void atecc_wake()
int8_t atecc_recv(uint8_t * buf, uint8_t buflen, struct atecc_response* res)
{
uint8_t pkt_len;
smb_init_crc();
pkt_len = smb_read( ATECC508A_ADDR,buf,buflen);
if (SMB_WAS_NACKED())
{

Wyświetl plik

@ -13,12 +13,15 @@
#include "bsp.h"
#include "app.h"
static void smb_init_crc();
uint8_t smb_read (uint8_t addr, uint8_t* dest, uint8_t count)
{
while(SMB_IS_BUSY()){}
SMB_FLAGS = SMB_READ | SMB_BUSY| SMB_preflags;
SMB_crc = 0;
SMB_crc_offset = 0;
SMB_FLAGS = SMB_READ | SMB_BUSY | SMB_preflags;
SMB_preflags = 0;
SMB_read_offset = 0;
@ -36,16 +39,18 @@ void smb_write (uint8_t addr, uint8_t* buf, uint8_t len)
{
while(SMB_IS_BUSY()){}
SMB_FLAGS = SMB_WRITE | SMB_BUSY | SMB_preflags;
SMB_preflags = 0;
SMB_crc = 0;
SMB_crc_offset = 0;
SMB_FLAGS = SMB_WRITE | SMB_BUSY | SMB_preflags;
SMB_preflags = 0;
SMB_write_len = len;
SMB_write_buf = buf;
SMB_write_offset = 0;
SMB_addr = addr;
SMB_write_len = len;
SMB_write_buf = buf;
SMB_write_offset = 0;
SMB_addr = addr;
SMB0CN0_STA = 1;
while(SMB_IS_BUSY()){}
SMB0CN0_STA = 1;
while(SMB_IS_BUSY()){}
}
void smb_set_ext_write( uint8_t* extbuf, uint8_t extlen)
@ -57,13 +62,6 @@ void smb_set_ext_write( uint8_t* extbuf, uint8_t extlen)
SMB_preflags |= SMB_WRITE_EXT;
}
void smb_init_crc()
{
while(SMB_IS_BUSY()){}
SMB_crc = 0;
SMB_crc_offset = 0;
SMB_preflags |= SMB_COMPUTE_CRC;
}
uint16_t feed_crc(uint16_t crc, uint8_t b)
{

Wyświetl plik

@ -71,7 +71,7 @@ void rgb(uint8_t r, uint8_t g, uint8_t b)
}
#define ms_since(ms,num) (((uint16_t)get_ms() - (ms)) >= num ? (1|(ms=(uint16_t)get_ms())):0)
#define ms_since(ms,num) (((uint16_t)get_ms() - (ms)) >= num ? ((ms=(uint16_t)get_ms())):0)
int16_t main(void) {
@ -97,7 +97,7 @@ int16_t main(void) {
u2f_prints("U2F ZERO\r\n");
if (RSTSRC & RSTSRC_WDTRSF__SET)
{
u2f_prints("damn watchdog\r\n");
error = ERROR_DAMN_WATCHDOG;
}
run_tests();
atecc_setup_init(appdata.tmp);
@ -143,11 +143,8 @@ int16_t main(void) {
}
break;
case APP_HID_MSG:
#ifndef ATECC_SETUP_DEVICE
u2f_hid_request(hid_msg);
#else
atecc_setup_device((struct config_msg*)appdata.hid_msg);
#endif
if (state == APP_HID_MSG)
state = APP_NOTHING;
break;
@ -178,6 +175,10 @@ int16_t main(void) {
u2f_printb("error: ", 1, error);
error = 0;
rgb(200,0,0);
while(!ms_since(ms_heart,2000))
{
watchdog();
}
}

Wyświetl plik

@ -17,6 +17,7 @@
#define CID_MAX (sizeof(CIDS)/sizeof(uint32_t))
typedef enum
{
HID_BUSY=0,
@ -43,16 +44,17 @@ static struct hid_layer_param
uint16_t bytes_written;
// total length of response in bytes
uint16_t res_len;
#define BUFFER_SIZE 200
uint8_t buffer[BUFFER_SIZE];
} hid_layer;
uint32_t _hid_lockt = 0;
uint32_t _hid_lock_cid = 0;
struct CID CIDS[5];
static uint8_t CID_NUM = 0;
@ -65,6 +67,9 @@ static uint8_t _hid_in_session = 0;
#define MIN(a,b) ((a) < (b) ? (a):(b))
#define hid_is_locked() (_hid_lockt > get_ms())
#define hid_is_lock_cid(c) ((c) == _hid_lock_cid)
void u2f_hid_init()
{
memset(CIDS, 0, sizeof(CIDS));
@ -246,6 +251,7 @@ static void hid_u2f_parse(struct u2f_hid_msg* req)
{
uint16_t len = 0;
uint8_t secs;
struct u2f_hid_init_response * init_res = appdata.tmp;
switch(hid_layer.current_cmd)
@ -343,8 +349,21 @@ static void hid_u2f_parse(struct u2f_hid_msg* req)
set_app_state(APP_WINK);
break;
case U2FHID_LOCK:
// TODO
//u2f_prints("U2F LOCK\r\n");
secs = req->pkt.init.payload[0];
if (secs > 10)
{
stamp_error(hid_layer.current_cid, ERR_INVALID_PAR);
}
else
{
_hid_lock_cid = hid_layer.current_cid;
_hid_lockt = get_ms() + 1000 * secs;
u2f_hid_set_len(0);
u2f_hid_writeback(NULL, 0);
u2f_hid_flush();
}
break;
default:
set_app_error(ERROR_HID_INVALID_CMD);
@ -362,6 +381,7 @@ static void hid_u2f_parse(struct u2f_hid_msg* req)
void u2f_hid_request(struct u2f_hid_msg* req)
{
uint8_t* payload = req->pkt.init.payload;
static int8_t last_seq = -1;
struct CID* cid = get_cid(req->cid);
@ -380,6 +400,16 @@ void u2f_hid_request(struct u2f_hid_msg* req)
return;
}
// ignore if we locked to a different cid
if(hid_is_locked())
{
if (!hid_is_lock_cid(cid))
{
stamp_error(hid_layer.current_cid, ERR_CHANNEL_BUSY);
return;
}
}
hid_layer.state = (u2f_hid_busy()) ? HID_BUSY : HID_READY;
switch(hid_layer.state)
@ -397,7 +427,7 @@ void u2f_hid_request(struct u2f_hid_msg* req)
hid_layer.current_cid = req->cid;
hid_layer.current_cmd = req->pkt.init.cmd;
hid_layer.last_buffered = get_ms();
last_seq = -1;
}
else
@ -414,11 +444,22 @@ void u2f_hid_request(struct u2f_hid_msg* req)
{
if (req->pkt.init.cmd & TYPE_INIT)
{
// TODO
//u2f_prints("this should resync but im lazy\r\n");
u2f_hid_reset_packet();
u2f_hid_request(req);
return;
}
hid_layer.last_buffered = get_ms();
// TODO verify packets arrive in ascending order
// verify packets arrive in ascending order
if (last_seq + 1 != req->pkt.cont.seq)
{
u2f_hid_reset_packet();
stamp_error(hid_layer.current_cid, ERR_INVALID_SEQ);
return;
}
last_seq = req->pkt.cont.seq;
}
else if (U2FHID_TIMEOUT(&hid_layer))
{