pass fido2 tests

This commit is contained in:
Conor Patrick
2018-10-28 16:30:55 -04:00
parent 2f911368da
commit 2fd96f8e4b
23 changed files with 900 additions and 318 deletions

View File

@@ -15,6 +15,7 @@
#include "util.h"
#include "fifo.h"
#include "log.h"
#include "ctaphid.h"
#define PAGE_SIZE 2048
@@ -34,18 +35,28 @@
#define AUTH_WORD_ADDR (flash_addr(APPLICATION_END_PAGE)-4)
uint32_t __65_seconds = 0;
uint32_t __90_ms = CTAPHID_STATUS_IDLE;
uint32_t __device_status = 0;
uint32_t __last_update = 0;
extern PCD_HandleTypeDef hpcd;
#define IS_BUTTON_PRESSED() (0 == (LL_GPIO_ReadInputPort(SOLO_BUTTON_PORT) & SOLO_BUTTON_PIN))
// Timer6 overflow handler
// Timer6 overflow handler. happens every ~90ms.
void TIM6_DAC_IRQHandler()
{
// timer is only 16 bits, so roll it over here
TIM6->SR = 0;
__65_seconds += 1;
__90_ms += 1;
if ((millis() - __last_update) > 5)
{
if (__device_status != CTAPHID_STATUS_IDLE)
{
ctaphid_update_status(__device_status);
}
}
}
// Global USB interrupt handler
void USB_IRQHandler(void)
{
@@ -55,10 +66,21 @@ void USB_IRQHandler(void)
uint32_t millis()
{
return (((uint32_t)TIM6->CNT) | (__65_seconds<<16));
return (((uint32_t)TIM6->CNT) + (__90_ms * 90));
}
void device_set_status(int status)
{
__disable_irq();
__last_update = millis();
__enable_irq();
if (status != CTAPHID_STATUS_IDLE && __device_status != status)
{
ctaphid_update_status(status);
}
__device_status = status;
}
void delay(uint32_t ms)
@@ -75,7 +97,6 @@ void device_init()
LL_GPIO_SetPinPull(SOLO_BUTTON_PORT,SOLO_BUTTON_PIN,LL_GPIO_PULL_UP);
printf1(TAG_GEN,"hello solo\r\n");
}
void usbhid_init()
@@ -86,7 +107,6 @@ int usbhid_recv(uint8_t * msg)
{
if (fifo_hidmsg_size())
{
fifo_hidmsg_take(msg);
printf1(TAG_DUMP,">> ");
dump_hex1(TAG_DUMP,msg, HID_PACKET_SIZE);
@@ -188,9 +208,11 @@ uint32_t ctap_atomic_count(int sel)
int offset = 0;
uint32_t * ptr = (uint32_t *)flash_addr(COUNTER1_PAGE);
uint32_t erases = *(uint32_t *)flash_addr(COUNTER2_PAGE);
static uint32_t sc = 0;
if (erases == 0xffffffff)
{
erases = 1;
flash_erase_page(COUNTER2_PAGE);
flash_write(flash_addr(COUNTER2_PAGE), (uint8_t*)&erases, 4);
}
@@ -220,44 +242,54 @@ uint32_t ctap_atomic_count(int sel)
if (!lastc) // Happens on initialization as well.
{
printf("warning, power interrupted during previous count. Restoring.\r\n");
printf2(TAG_ERR,"warning, power interrupted during previous count. Restoring. lastc==%lu, erases=%lu, offset=%d\r\n", lastc,erases,offset);
// there are 32 counts per page
lastc = erases * 32;
lastc = erases * 256 + 1;
flash_erase_page(COUNTER1_PAGE);
flash_write(flash_addr(COUNTER1_PAGE), (uint8_t*)&lastc, 4);
erases++;
flash_erase_page(COUNTER2_PAGE);
flash_write(flash_addr(COUNTER2_PAGE), (uint8_t*)&erases, 4);
return lastc;
}
lastc++;
if (lastc/32 > erases)
if (lastc/256 > erases)
{
printf("warning, power interrupted, erases mark, restoring\r\n");
erases = lastc/32 + 1;
printf2(TAG_ERR,"warning, power interrupted, erases mark, restoring. lastc==%lu, erases=%lu\r\n", lastc,erases);
erases = lastc/256;
flash_erase_page(COUNTER2_PAGE);
flash_write(flash_addr(COUNTER2_PAGE), (uint8_t*)&erases, 4);
}
if (offset == PAGE_SIZE/4)
{
if (lastc/32 > erases)
if (lastc/256 > erases)
{
printf("warning, power interrupted, erases mark, restoring\r\n");
printf2(TAG_ERR,"warning, power interrupted, erases mark, restoring lastc==%lu, erases=%lu\r\n", lastc,erases);
}
erases = lastc/32 + 1;
erases = lastc/256 + 1;
flash_erase_page(COUNTER2_PAGE);
flash_write(flash_addr(COUNTER2_PAGE), (uint8_t*)&erases, 4);
flash_erase_page(COUNTER1_PAGE);
offset = 0;
}
else
flash_write(flash_addr(COUNTER1_PAGE) + offset * 4, (uint8_t*)&lastc, 4);
if (lastc == sc)
{
flash_write(flash_addr(COUNTER1_PAGE) + offset * 4, (uint8_t*)&lastc, 4);
printf1(TAG_RED,"no count detected: lastc==%lu, erases=%lu, offset=%d\r\n", lastc,erases,offset);
while(1)
;
}
sc = lastc;
return lastc;
}
@@ -284,10 +316,38 @@ void device_manage()
#endif
}
static int handle_packets()
{
static uint8_t hidmsg[HID_PACKET_SIZE];
memset(hidmsg,0, sizeof(hidmsg));
if (usbhid_recv(hidmsg) > 0)
{
if ( ctaphid_handle_packet(hidmsg) == CTAPHID_CANCEL)
{
printf1(TAG_GREEN, "CANCEL!\r\n");
return -1;
}
else
{
return 0;
}
}
return 0;
}
int ctap_user_presence_test()
{
int oldstatus = __device_status;
int ret;
#if SKIP_BUTTON_CHECK
return 1;
int i=500;
while(i--)
{
delay(1);
ret = handle_packets();
if (ret) return ret;
}
goto done;
#endif
uint32_t t1 = millis();
@@ -297,15 +357,17 @@ int ctap_user_presence_test()
delay(3000);
led_rgb(0x001040);
delay(50);
return 1;
goto done;
#endif
while (IS_BUTTON_PRESSED())
{
if (t1 + 5000 < millis())
{
printf1(TAG_GEN,"Button not pressed\n");
return 0;
goto fail;
}
ret = handle_packets();
if (ret) return ret;
}
t1 = millis();
@@ -314,11 +376,13 @@ do
{
if (t1 + 5000 < millis())
{
return 0;
goto fail;
}
if (! IS_BUTTON_PRESSED())
continue;
delay(1);
ret = handle_packets();
if (ret) return ret;
}
while (! IS_BUTTON_PRESSED());
@@ -326,7 +390,11 @@ led_rgb(0x001040);
delay(50);
done:
return 1;
fail:
return 0;
}
int ctap_generate_rng(uint8_t * dst, size_t num)

View File

@@ -6,7 +6,7 @@
FIFO_CREATE(debug,1024,1)
FIFO_CREATE(hidmsg,100,100)
FIFO_CREATE(hidmsg,100,64)
#if TEST_FIFO
FIFO_CREATE(test,10,100)
@@ -24,23 +24,25 @@ void fifo_test()
for (int i = 0; i < 10; i++)
{
printf("rhead: %d, whead: %d\r\n", fifo_test_rhead(), fifo_test_whead());
ret = fifo_test_add(data[i]);
printf("%d\r\n",i);
if (ret != 0)
{
printf("fifo_test_add fail\r\n");
goto end;
goto fail;
}
}
for (int i = 0; i < 10; i++)
{
printf("rhead: %d, whead: %d\r\n", fifo_test_rhead(), fifo_test_whead());
ret = fifo_test_take(verif[i]);
printf("%d\r\n",i );
if (ret != 0)
{
printf("fifo_test_take fail\r\n");
goto end;
goto fail;
}
if (memcmp(verif[i], data[i], 100) != 0)
@@ -48,17 +50,18 @@ void fifo_test()
printf("fifo_test_take result fail\r\n");
dump_hex(data[i],100);
dump_hex(verif[i],100);
goto end;
goto fail;
}
}
for (int i = 0; i < 10; i++)
{
printf("rhead: %d, whead: %d\r\n", fifo_test_rhead(), fifo_test_whead());
ret = fifo_test_add(data[i]);
if (ret != 0)
{
printf("fifo_test_add 2 fail\r\n");
goto end;
goto fail;
}
}
@@ -66,22 +69,25 @@ void fifo_test()
if (ret == 0)
{
printf("fifo_test_add should have failed\r\n");
goto end;
goto fail;
}
for (int i = 0; i < 10; i++)
{
printf("rhead: %d, whead: %d\r\n", fifo_test_rhead(), fifo_test_whead());
ret = fifo_test_take(verif[i]);
if (ret != 0)
{
printf("fifo_test_take fail\r\n");
goto end;
goto fail;
}
if (memcmp(verif[i], data[i], 100) != 0)
{
printf("fifo_test_take result fail\r\n");
goto end;
goto fail;
}
}
@@ -89,12 +95,12 @@ void fifo_test()
if (ret == 0)
{
printf("fifo_test_take should have failed\r\n");
goto end;
goto fail;
}
printf("test pass!\r\n");
end:
return ;
fail:
while(1)
;
}

View File

@@ -3,7 +3,9 @@
#include "app.h"
#ifndef TEST_FIFO
#define TEST_FIFO 0
#endif
#define FIFO_CREATE(NAME,LENGTH,BYTES)\
int __##NAME##_WRITE_PTR = 0;\
@@ -13,7 +15,7 @@ static uint8_t __##NAME##_WRITE_BUF[BYTES * LENGTH];\
\
int fifo_##NAME##_add(uint8_t * c)\
{\
if (__##NAME##_WRITE_PTR != __##NAME##_READ_PTR || !__##NAME##_SIZE)\
if (__##NAME##_SIZE < LENGTH)\
{\
memmove(__##NAME##_WRITE_BUF + __##NAME##_WRITE_PTR * BYTES, c, BYTES);\
__##NAME##_WRITE_PTR ++;\
@@ -28,7 +30,7 @@ int fifo_##NAME##_add(uint8_t * c)\
int fifo_##NAME##_take(uint8_t * c)\
{\
memmove(c, __##NAME##_WRITE_BUF + __##NAME##_READ_PTR * BYTES, BYTES);\
if (__##NAME##_READ_PTR != __##NAME##_WRITE_PTR || __##NAME##_SIZE)\
if ( __##NAME##_SIZE > 0)\
{\
__##NAME##_READ_PTR ++;\
if (__##NAME##_READ_PTR >= LENGTH)\
@@ -43,17 +45,27 @@ uint32_t fifo_##NAME##_size()\
{\
return (__##NAME##_SIZE);\
}\
uint32_t fifo_##NAME##_rhead()\
{\
return (__##NAME##_READ_PTR);\
}\
uint32_t fifo_##NAME##_whead()\
{\
return (__##NAME##_WRITE_PTR);\
}\
#define FIFO_CREATE_H(NAME,LENGTH,BYTES)\
#define FIFO_CREATE_H(NAME)\
int fifo_##NAME##_add(uint8_t * c);\
int fifo_##NAME##_take(uint8_t * c);\
uint32_t fifo_##NAME##_size();\
uint32_t fifo_##NAME##_rhead();\
uint32_t fifo_##NAME##_whead();\
FIFO_CREATE_H(hidmsg,10,64)
FIFO_CREATE_H(hidmsg)
FIFO_CREATE_H(debug,1024,1)
FIFO_CREATE_H(debug)
FIFO_CREATE_H(test,100,100)
FIFO_CREATE_H(test)
void fifo_test();

View File

@@ -18,6 +18,7 @@ static void flash_unlock()
void flash_erase_page(uint8_t page)
{
__disable_irq();
flash_unlock();
// Wait if flash is busy
while (FLASH->SR & (1<<16))
;
@@ -71,9 +72,10 @@ void flash_write(uint32_t addr, uint8_t * data, size_t sz)
{
int i;
uint8_t buf[8];
flash_unlock();
// dword align
addr &= ~(0x7);
addr &= ~(0x07);
for(i = 0; i < sz; i+=8)
{

View File

@@ -337,10 +337,10 @@ static void MX_TIM6_Init(void)
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM6);
// 48 MHz sys clock --> 6 MHz timer clock
// 6 MHz / 6000 == 1000 Hz
// 48 MHz / 48000 == 1000 Hz
TIM_InitStruct.Prescaler = 48000;
TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
TIM_InitStruct.Autoreload = 0xffff;
TIM_InitStruct.Autoreload = 90;
LL_TIM_Init(TIM6, &TIM_InitStruct);
LL_TIM_DisableARRPreload(TIM6);
@@ -356,6 +356,35 @@ static void MX_TIM6_Init(void)
LL_TIM_EnableCounter(TIM6);
}
/* TIM7 init function */
// static void MX_TIM7_Init(void)
// {
//
// LL_TIM_InitTypeDef TIM_InitStruct;
//
// /* Peripheral clock enable */
// LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM7);
//
// // 48 MHz sys clock --> 6 MHz timer clock
// // 6 MHz / 6000 == 1000 Hz
// TIM_InitStruct.Prescaler = 48000;
// TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
// TIM_InitStruct.Autoreload = 0xffff;
// LL_TIM_Init(TIM6, &TIM_InitStruct);
//
// LL_TIM_DisableARRPreload(TIM7);
//
// LL_TIM_SetTriggerOutput(TIM7, LL_TIM_TRGO_RESET);
//
// LL_TIM_DisableMasterSlaveMode(TIM7);
//
// // enable interrupt
// TIM7->DIER |= 1;
//
// // Start immediately
// LL_TIM_EnableCounter(TIM7);
// }
/* RNG init function */
static void MX_RNG_Init(void)
{

View File

@@ -27,9 +27,126 @@
#include "device.h"
#include "util.h"
#include "fifo.h"
#include "log.h"
#ifdef TEST_SOLO_STM32
#define Error_Handler() _Error_Handler(__FILE__,__LINE__)
#define PAGE_SIZE 2048
#define PAGES 128
// Pages 119-127 are data
#define COUNTER2_PAGE (PAGES - 4)
#define COUNTER1_PAGE (PAGES - 3)
#define STATE2_PAGE (PAGES - 2)
#define STATE1_PAGE (PAGES - 1)
void test_atomic_counter()
{
// flash_erase_page(COUNTER1_PAGE);
// flash_erase_page(COUNTER2_PAGE);
int i;
uint32_t c0 = ctap_atomic_count(0);
for (i = 0; i < 128; i++)
{
uint32_t c1 = ctap_atomic_count(0);
if (c1 <= (c0 ))
{
printf("error, count failed %lu <= %lu\r\n",c1,c0);
while(1)
;
}
printf("%lu\r\n", c1);
c0 = c1;
}
printf("test faults\r\n");
printf("%lu\r\n", ctap_atomic_count(0));
printf("%lu\r\n", ctap_atomic_count(0));
printf("%lu\r\n", ctap_atomic_count(0));
printf("%lu\r\n", ctap_atomic_count(0));
printf("%lu\r\n", ctap_atomic_count(0));
flash_erase_page(COUNTER1_PAGE);
printf("%lu\r\n", ctap_atomic_count(0));
printf("%lu\r\n", ctap_atomic_count(0));
printf("%lu\r\n", ctap_atomic_count(0));
printf("%lu\r\n", ctap_atomic_count(0));
printf("%lu\r\n", ctap_atomic_count(0));
flash_erase_page(COUNTER1_PAGE);
printf("%lu\r\n", ctap_atomic_count(0));
printf("%lu\r\n", ctap_atomic_count(0));
printf("%lu\r\n", ctap_atomic_count(0));
printf("%lu\r\n", ctap_atomic_count(0));
printf("%lu\r\n", ctap_atomic_count(0));
flash_erase_page(COUNTER1_PAGE);
printf("%lu\r\n", ctap_atomic_count(0));
printf("%lu\r\n", ctap_atomic_count(0));
printf("%lu\r\n", ctap_atomic_count(0));
printf("%lu\r\n", ctap_atomic_count(0));
printf("%lu\r\n", ctap_atomic_count(0));
flash_erase_page(COUNTER1_PAGE);
printf("%lu\r\n", ctap_atomic_count(0));
printf("%lu\r\n", ctap_atomic_count(0));
printf("%lu\r\n", ctap_atomic_count(0));
printf("%lu\r\n", ctap_atomic_count(0));
printf("%lu\r\n", ctap_atomic_count(0));
flash_erase_page(COUNTER2_PAGE);
printf("%lu\r\n", ctap_atomic_count(0));
printf("%lu\r\n", ctap_atomic_count(0));
printf("%lu\r\n", ctap_atomic_count(0));
printf("%lu\r\n", ctap_atomic_count(0));
printf("%lu\r\n", ctap_atomic_count(0));
flash_erase_page(COUNTER2_PAGE);
printf("%lu\r\n", ctap_atomic_count(0));
printf("%lu\r\n", ctap_atomic_count(0));
printf("%lu\r\n", ctap_atomic_count(0));
printf("%lu\r\n", ctap_atomic_count(0));
printf("%lu\r\n", ctap_atomic_count(0));
flash_erase_page(COUNTER2_PAGE);
printf("%lu\r\n", ctap_atomic_count(0));
printf("%lu\r\n", ctap_atomic_count(0));
printf("%lu\r\n", ctap_atomic_count(0));
printf("%lu\r\n", ctap_atomic_count(0));
printf("%lu\r\n", ctap_atomic_count(0));
flash_erase_page(COUNTER2_PAGE);
printf("%lu\r\n", ctap_atomic_count(0));
printf("%lu\r\n", ctap_atomic_count(0));
printf("%lu\r\n", ctap_atomic_count(0));
printf("%lu\r\n", ctap_atomic_count(0));
printf("%lu\r\n", ctap_atomic_count(0));
flash_erase_page(COUNTER1_PAGE);
printf("%lu\r\n", ctap_atomic_count(0));
printf("%lu\r\n", ctap_atomic_count(0));
printf("%lu\r\n", ctap_atomic_count(0));
printf("%lu\r\n", ctap_atomic_count(0));
printf("%lu\r\n", ctap_atomic_count(0));
flash_erase_page(COUNTER1_PAGE);
printf("%lu\r\n", ctap_atomic_count(0));
printf("%lu\r\n", ctap_atomic_count(0));
printf("%lu\r\n", ctap_atomic_count(0));
printf("%lu\r\n", ctap_atomic_count(0));
printf("%lu\r\n", ctap_atomic_count(0));
flash_erase_page(COUNTER1_PAGE);
}
int main(void)
{
@@ -41,7 +158,24 @@ int main(void)
uint8_t hidbuf[HID_PACKET_SIZE];
hw_init();
set_logging_mask(
/*0*/
// TAG_GEN|
TAG_MC |
TAG_GA |
// TAG_WALLET |
TAG_STOR |
TAG_CP |
TAG_CTAP|
// TAG_HID|
/*TAG_U2F|*/
TAG_PARSE |
//TAG_TIME|
// TAG_DUMP|
TAG_GREEN|
TAG_RED|
TAG_ERR
);
printf("hello solo\r\n");
// Test flash
@@ -50,6 +184,24 @@ int main(void)
memmove(buf,(uint8_t*)flash_addr(60),sizeof(str));
printf("flash: \"%s\"\r\n", buf);
// test_atomic_counter();
// Note that 4 byte aligned addresses won't get written correctly.
flash_erase_page(60);
uint32_t count = 0;
flash_write(flash_addr(60) + 0,(uint8_t*)&count,4);
count += 1;
flash_write(flash_addr(60) + 4,(uint8_t*)&count,4);
count += 1;
flash_write(flash_addr(60) + 8,(uint8_t*)&count,4);
count += 1;
flash_write(flash_addr(60) + 12,(uint8_t*)&count,4);
count += 1;
flash_write(flash_addr(60) + 16,(uint8_t*)&count,4);
dump_hex((uint8_t *)flash_addr(60), 20);
// test timer
uint32_t t1 = millis();
delay(100);
@@ -63,7 +215,7 @@ int main(void)
/*// Test PWM + weighting of RGB*/
/*led_test_colors();*/
fifo_test();
uint32_t t0 = millis();
@@ -91,15 +243,9 @@ int main(void)
fifo_hidmsg_take(hidbuf);
dump_hex(hidbuf, HID_PACKET_SIZE);
}
while(1)
;
}
}
void _Error_Handler(char *file, int line)
{
printf("Error: %s: %d\r\n", file, line);
while(1)
{
}
}
#endif