initialize keys at init if needed

vtcsec_summit
Conor 2016-03-04 20:30:13 -05:00
rodzic 86a56dade4
commit b51bd20c4d
6 zmienionych plików z 101 dodań i 11 usunięć

Wyświetl plik

@ -17,8 +17,6 @@ SI_SBIT (LED1, SFR_P1, 4); // LED green
// debug options
#define U2F_PRINT
#define KEYHANDLES_START (EEPROM_DATA_START + 30)
#define KEYHANDLES_COUNT 14
typedef enum
{
@ -47,17 +45,22 @@ APP_ERROR_CODE;
struct APP_DATA
{
uint8_t hidmsgbuf[64];
uint8_t tmp[10];
// must be at least 37 bytes
uint8_t tmp[40];
uint8_t state;
uint8_t error;
};
extern uint8_t hidmsgbuf[64];
extern data struct APP_DATA appdata;
void set_app_error(APP_ERROR_CODE ec);
// should be called after initializing eeprom
void u2f_init();

Wyświetl plik

@ -8,6 +8,10 @@
#ifndef EEPROM_H_
#define EEPROM_H_
#include "app.h"
void eeprom_init();
void eeprom_read(uint16_t addr, uint8_t * buf, uint8_t len);
void _eeprom_write(uint16_t addr, uint8_t * buf, uint8_t len, uint8_t flags);

Wyświetl plik

@ -26,7 +26,7 @@
uint8_t tmpBuffer;
#define PRINT_EVENTS
//#define PRINT_EVENTS
#ifdef PRINT_EVENTS
@ -144,21 +144,24 @@ USB_Status_TypeDef USBD_SetupCmdCb(
}
uint8_t hidmsgbuf[64];
uint16_t USBD_XferCompleteCb(uint8_t epAddr, USB_Status_TypeDef status,
uint16_t xferred, uint16_t remaining) {
if (epAddr == EP1OUT)
{
#ifdef U2F_PRINT
int i = 0;
for (i=0; i < sizeof(appdata.hidmsgbuf); i++)
for (i=0; i < sizeof(hidmsgbuf); i++)
{
uint8_t l = (uint8_t)appdata.hidmsgbuf[i];
uint8_t l = (uint8_t)hidmsgbuf[i];
u2f_putb(l);
}
u2f_prints("\r\n");
#endif
u2f_hid_request((struct u2f_hid_msg*)appdata.hidmsgbuf);
u2f_hid_request((struct u2f_hid_msg*)hidmsgbuf);
}

Wyświetl plik

@ -9,7 +9,17 @@
#include "eeprom.h"
void eeprom_init()
{
uint8_t secbyte;
eeprom_read(0xFBFF,&secbyte,1);
if (secbyte == 0xff)
{
eeprom_erase(0xFBC0);
secbyte = -32;
eeprom_write(0xFBFF, &secbyte, 1);
}
}
void eeprom_read(uint16_t addr, uint8_t * buf, uint8_t len)
{

Wyświetl plik

@ -24,6 +24,8 @@ static void init(struct APP_DATA* ap)
u2f_hid_init();
smb_init();
atecc_idle();
eeprom_init();
u2f_init();
}
void set_app_error(APP_ERROR_CODE ec)
@ -32,9 +34,27 @@ void set_app_error(APP_ERROR_CODE ec)
appdata.error = ec;
}
void dump_eeprom()
{
// 0xF800 - 0xFB7F
uint16_t i = 0xF800;
uint8_t eep;
for (; i <= 0xF800 + 196; i++)
{
eeprom_read(i,&eep,1);
u2f_putb(eep);
u2f_prints(" ");
}
u2f_prints("\r\n");
}
int8_t test_eeprom()
{
dump_eeprom();
return 0;
}
#define ms_since(ms,num) (((uint16_t)get_ms() - (ms)) >= num ? (1|(ms=(uint16_t)get_ms())):0)
@ -74,7 +94,7 @@ int16_t main(void) {
{
if (!USBD_EpIsBusy(EP1OUT) && !USBD_EpIsBusy(EP1IN))
{
USBD_Read(EP1OUT, appdata.hidmsgbuf, sizeof(appdata.hidmsgbuf), true);
USBD_Read(EP1OUT, hidmsgbuf, sizeof(hidmsgbuf), true);
u2f_prints("read added\r\n");
}

Wyświetl plik

@ -8,19 +8,69 @@
#include "bsp.h"
#include "u2f.h"
#include "u2f_hid.h"
#include "eeprom.h"
#include "atecc508a.h"
struct key_handle
{
uint8_t index;
uint8_t entropy[3];
};
struct key_storage_header
{
uint8_t num_keys;
uint16_t valid_keys;
uint8_t num_issued;
} key_store;
#define U2F_NUM_KEYS 14
#define U2F_KEY_HEADER_ADDR 0xF800
#define U2F_KEYS_ADDR (0xF800 + sizeof(struct key_storage_header))
#define IS_KEY_VALID(mask,key) ((~mask) & (1<<key))
static struct u2f_hid_msg res;
static uint8_t* resbuf = (uint8_t*)&res;
static uint8_t resoffset = 0;
static uint8_t resseq = 0;
static void flush_key_store()
{
eeprom_write(U2F_KEY_HEADER_ADDR, (uint8_t* )&key_store, sizeof(struct key_storage_header));
}
void u2f_init()
{
uint8_t i,j;
struct atecc_response res;
eeprom_read(U2F_KEY_HEADER_ADDR, (uint8_t* )&key_store, sizeof(struct key_storage_header));
// initialize key handles
if (key_store.num_keys != U2F_NUM_KEYS)
{
key_store.num_keys = U2F_NUM_KEYS;
key_store.valid_keys = 0;
key_store.num_issued = 0;
flush_key_store();
for (i=0; i < 2; i++)
{
atecc_send_recv(ATECC_CMD_RNG,ATECC_RNG_P1,ATECC_RNG_P2,
NULL, 0,
appdata.tmp,
sizeof(appdata.tmp), &res);
for (j=0; j < U2F_NUM_KEYS/2; j++) res.buf[j * U2F_KEY_HANDLE_SIZE] = j+1 + i*U2F_NUM_KEYS/2;
eeprom_write(U2F_KEYS_ADDR + i * (U2F_KEY_HANDLE_SIZE * U2F_NUM_KEYS/2),
res.buf, U2F_KEY_HANDLE_SIZE * U2F_NUM_KEYS/2);
}
}
}
void u2f_response_writeback(uint8_t * buf, uint8_t len)
{
u2f_hid_writeback(buf, len);
@ -91,7 +141,7 @@ void u2f_new_keypair(uint8_t * handle, uint8_t * pubkey)
}
code char __attest[] = ""
code char __attest[] =
"\x30\x82\x01\x72\x30\x82\x01\x18\x02\x01\x01\x30\x0a\x06\x08\x2a\x86\x48\xce\x3d"
"\x04\x03\x02\x30\x45\x31\x0b\x30\x09\x06\x03\x55\x04\x06\x13\x02\x41\x55\x31\x13"
"\x30\x11\x06\x03\x55\x04\x08\x0c\x0a\x53\x6f\x6d\x65\x2d\x53\x74\x61\x74\x65\x31"