kopia lustrzana https://github.com/conorpp/u2f-zero
rewrote eeprom and added test
rodzic
c3569fde3d
commit
a809d9dc37
|
|
@ -19,6 +19,9 @@ SI_SBIT (LED1, SFR_P1, 4); // LED green
|
|||
#define DBG_MESSAGE_SIZE 30
|
||||
#define DBG_MSG_COUNT 4
|
||||
|
||||
#define KEYHANDLES_START (EEPROM_DATA_START + 30)
|
||||
#define KEYHANDLES_COUNT 14
|
||||
|
||||
typedef enum
|
||||
{
|
||||
APP_NOTHING = 0,
|
||||
|
|
@ -61,13 +64,7 @@ struct debug_msg
|
|||
|
||||
void set_app_error(APP_ERROR_CODE ec);
|
||||
|
||||
#define DISABLE_INTERUPTS() (EIE1 = EIE1_EADC0__DISABLED | EIE1_EWADC0__DISABLED | EIE1_ECP0__DISABLED\
|
||||
| EIE1_ECP1__DISABLED | EIE1_EMAT__DISABLED | EIE1_EPCA0__DISABLED\
|
||||
| EIE1_ESMB0__ENABLED | EIE1_ET3__ENABLED)
|
||||
|
||||
#define ENABLE_INTERUPTS() (IE = IE_EA__ENABLED | IE_EX0__DISABLED | IE_EX1__DISABLED\
|
||||
| IE_ESPI0__DISABLED | IE_ET0__DISABLED | IE_ET1__DISABLED\
|
||||
| IE_ET2__ENABLED | IE_ES0__DISABLED)
|
||||
|
||||
|
||||
#define FIFO_HEADER(NAME, TYPE)\
|
||||
|
|
|
|||
|
|
@ -65,7 +65,8 @@ void u2f_printlx(const char * tag, uint8_t c, ...);
|
|||
|
||||
#else
|
||||
#define u2f_print(x)
|
||||
#define dump_hex(x)
|
||||
// #define dump_hex(x)
|
||||
void dump_hex(uint8_t* hex, uint8_t len);
|
||||
#define flush_messages(x)
|
||||
#endif
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,20 @@
|
|||
/*
|
||||
* eeprom.h
|
||||
*
|
||||
* Created on: Mar 4, 2016
|
||||
* Author: pp
|
||||
*/
|
||||
|
||||
#ifndef EEPROM_H_
|
||||
#define EEPROM_H_
|
||||
|
||||
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);
|
||||
|
||||
#define eeprom_write(a,b,l) _eeprom_write(a,b,l,0x1)
|
||||
#define eeprom_erase(a) _eeprom_write(a,appdata.tmp,1,0x3)
|
||||
|
||||
#define EEPROM_DATA_START 0xF800
|
||||
|
||||
#endif /* EEPROM_H_ */
|
||||
|
|
@ -514,7 +514,9 @@ extern void INTERRUPT_0_enter_DefaultMode_from_RESET(void) {
|
|||
// ET3 (Timer 3 Interrupt Enable) = ENABLED (Enable interrupt requests
|
||||
// generated by the TF3L or TF3H flags.)
|
||||
*/
|
||||
DISABLE_INTERUPTS();
|
||||
EIE1 = EIE1_EADC0__DISABLED | EIE1_EWADC0__DISABLED | EIE1_ECP0__DISABLED\
|
||||
| EIE1_ECP1__DISABLED | EIE1_EMAT__DISABLED | EIE1_EPCA0__DISABLED\
|
||||
| EIE1_ESMB0__ENABLED | EIE1_ET3__ENABLED;
|
||||
// [EIE1 - Extended Interrupt Enable 1]$
|
||||
|
||||
// $[EIE2 - Extended Interrupt Enable 2]
|
||||
|
|
@ -550,7 +552,9 @@ extern void INTERRUPT_0_enter_DefaultMode_from_RESET(void) {
|
|||
// generated by the TF2L or TF2H flags.)
|
||||
// ES0 (UART0 Interrupt Enable) = DISABLED (Disable UART0 interrupt.)
|
||||
*/
|
||||
ENABLE_INTERUPTS();
|
||||
IE = IE_EA__ENABLED | IE_EX0__DISABLED | IE_EX1__DISABLED\
|
||||
| IE_ESPI0__DISABLED | IE_ET0__DISABLED | IE_ET1__DISABLED\
|
||||
| IE_ET2__ENABLED | IE_ES0__DISABLED;
|
||||
// [IE - Interrupt Enable]$
|
||||
|
||||
// $[IP - Interrupt Priority]
|
||||
|
|
|
|||
|
|
@ -61,6 +61,18 @@ void flush_messages()
|
|||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
void dump_hex(uint8_t* hex, uint8_t len)
|
||||
{
|
||||
uint8_t i;
|
||||
for (i=0 ; i < len ; i++)
|
||||
{
|
||||
u2f_putb(hex[i]);
|
||||
}
|
||||
u2f_prints("\r\n");
|
||||
}
|
||||
|
||||
void putf(char c)
|
||||
{
|
||||
uint8_t i;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,53 @@
|
|||
/*
|
||||
* eeprom.c
|
||||
*
|
||||
* Created on: Mar 4, 2016
|
||||
* Author: pp
|
||||
*/
|
||||
#include <SI_EFM8UB1_Register_Enums.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "eeprom.h"
|
||||
|
||||
|
||||
|
||||
void eeprom_read(uint16_t addr, uint8_t * buf, uint8_t len)
|
||||
{
|
||||
uint8_t code * eepaddr = (uint8_t code *) addr;
|
||||
bit old_int;
|
||||
|
||||
while(len--)
|
||||
{
|
||||
old_int = IE_EA;
|
||||
IE_EA = 0;
|
||||
*buf++ = *eepaddr++;
|
||||
IE_EA = old_int;
|
||||
}
|
||||
}
|
||||
|
||||
void _eeprom_write(uint16_t addr, uint8_t * buf, uint8_t len, uint8_t flags)
|
||||
{
|
||||
uint8_t xdata * data eepaddr = (uint8_t xdata *) addr;
|
||||
bit old_int;// IE_EA;
|
||||
|
||||
while(len--)
|
||||
{
|
||||
old_int = IE_EA;
|
||||
IE_EA = 0;
|
||||
// Enable VDD monitor
|
||||
VDM0CN = 0x80;
|
||||
RSTSRC = 0x02;
|
||||
|
||||
// unlock key
|
||||
FLKEY = 0xA5;
|
||||
FLKEY = 0xF1;
|
||||
PSCTL |= flags;
|
||||
|
||||
*eepaddr = *buf;
|
||||
PSCTL &= ~flags;
|
||||
IE_EA = old_int;
|
||||
|
||||
eepaddr++;
|
||||
buf++;
|
||||
}
|
||||
}
|
||||
|
|
@ -6,6 +6,7 @@
|
|||
#include "atecc508a.h"
|
||||
#include "InitDevice.h"
|
||||
#include "descriptors.h"
|
||||
#include "eeprom.h"
|
||||
#include "idle.h"
|
||||
#include "bsp.h"
|
||||
#include "app.h"
|
||||
|
|
@ -14,7 +15,6 @@
|
|||
#include "u2f.h"
|
||||
#include "tests.h"
|
||||
|
||||
|
||||
data struct APP_DATA appdata;
|
||||
|
||||
|
||||
|
|
@ -45,134 +45,34 @@ void set_app_error(APP_ERROR_CODE ec)
|
|||
appdata.error = ec;
|
||||
}
|
||||
|
||||
uint8_t readByte (uint16_t addr)
|
||||
{
|
||||
//uint8_t readByte (uint16_t addr)
|
||||
//{
|
||||
//
|
||||
// uint8_t code * pread; // FLASH read pointer
|
||||
// uint8_t byte;
|
||||
// DISABLE_INTERUPTS();
|
||||
// pread = (uint8_t code *) addr;
|
||||
//
|
||||
// byte = *pread; // Read the byte
|
||||
//
|
||||
// ENABLE_INTERUPTS();
|
||||
//
|
||||
// return byte;
|
||||
//}
|
||||
|
||||
uint8_t code * pread; // FLASH read pointer
|
||||
uint8_t byte;
|
||||
DISABLE_INTERUPTS();
|
||||
pread = (uint8_t code *) addr;
|
||||
|
||||
byte = *pread; // Read the byte
|
||||
|
||||
ENABLE_INTERUPTS();
|
||||
|
||||
return byte;
|
||||
}
|
||||
|
||||
void writeByte (uint16_t addr, uint8_t byte)
|
||||
{
|
||||
bit EA_SAVE = IE_EA; // Preserve IE_EA
|
||||
uint8_t xdata * data pwrite; // Flash write pointer
|
||||
|
||||
IE_EA = 0; // Disable interrupts
|
||||
|
||||
VDM0CN = 0x80; // Enable VDD monitor
|
||||
|
||||
RSTSRC = 0x02; // Enable VDD monitor as a reset source
|
||||
|
||||
pwrite = (uint8_t xdata *) addr;
|
||||
|
||||
FLKEY = 0xA5; // Key Sequence 1
|
||||
FLKEY = 0xF1; // Key Sequence 2
|
||||
PSCTL |= 0x01; // PSWE = 1 which enables writes
|
||||
|
||||
VDM0CN = 0x80; // Enable VDD monitor
|
||||
|
||||
RSTSRC = 0x02; // Enable VDD monitor as a reset source
|
||||
*pwrite = byte; // Write the byte
|
||||
|
||||
PSCTL &= ~0x01; // PSWE = 0 which disable writes
|
||||
|
||||
IE_EA = EA_SAVE; // Restore interrupts
|
||||
}
|
||||
|
||||
void FLASH_PageErase (uint16_t addr)
|
||||
{
|
||||
bit EA_SAVE = IE_EA; // Preserve IE_EA
|
||||
uint8_t xdata * data pwrite; // Flash write pointer
|
||||
|
||||
IE_EA = 0; // Disable interrupts
|
||||
|
||||
VDM0CN = 0x80; // Enable VDD monitor
|
||||
|
||||
RSTSRC = 0x02; // Enable VDD monitor as a reset source
|
||||
|
||||
pwrite = (uint8_t xdata *) addr;
|
||||
|
||||
FLKEY = 0xA5; // Key Sequence 1
|
||||
FLKEY = 0xF1; // Key Sequence 2
|
||||
PSCTL |= 0x03; // PSWE = 1; PSEE = 1
|
||||
|
||||
VDM0CN = 0x80; // Enable VDD monitor
|
||||
|
||||
RSTSRC = 0x02; // Enable VDD monitor as a reset source
|
||||
*pwrite = 0; // Initiate page erase
|
||||
|
||||
PSCTL &= ~0x03; // PSWE = 0; PSEE = 0
|
||||
|
||||
IE_EA = EA_SAVE; // Restore interrupts
|
||||
}
|
||||
|
||||
#define EEPROM_DATA_START 0xF800
|
||||
#define KEYHANDLES_START (EEPROM_DATA_START + 30)
|
||||
#define KEYHANDLES_COUNT 14
|
||||
|
||||
void dump_eeprom()
|
||||
{
|
||||
// 0xF800 - 0xFB7F
|
||||
uint16_t i = 0xF800;
|
||||
uint8_t eep;
|
||||
for (; i <= 0xFBFF; i++)
|
||||
{
|
||||
eep = readByte(i);
|
||||
u2f_putb(eep);
|
||||
}
|
||||
|
||||
for (i=0; i <= 0x100; i++)
|
||||
{
|
||||
eep = readByte(i);
|
||||
u2f_putb(eep);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
int8_t test_eeprom()
|
||||
{
|
||||
uint8_t secbyte;
|
||||
uint16_t i;
|
||||
|
||||
// Enable flash erasing, then start a write cycle on the selected page
|
||||
secbyte = readByte(0xFBFF);
|
||||
u2f_printb("security_byte: ",1,secbyte);
|
||||
// PSCTL |= PSCTL_PSEE__ERASE_ENABLED;
|
||||
if (secbyte == 0xff)
|
||||
{
|
||||
FLASH_PageErase(0xFBC0);
|
||||
writeByte(0xFBFF, -32);
|
||||
}
|
||||
else
|
||||
{
|
||||
writeByte(KEYHANDLES_START, 0x55);
|
||||
for (i = 0; i < KEYHANDLES_COUNT ; i++)
|
||||
{
|
||||
writeByte(KEYHANDLES_START+(i<<2)+0, 0x55);
|
||||
writeByte(KEYHANDLES_START+(i<<2)+1, 0x66);
|
||||
writeByte(KEYHANDLES_START+(i<<2)+2, 0x77);
|
||||
writeByte(KEYHANDLES_START+(i<<2)+3, 0x88);
|
||||
}
|
||||
}
|
||||
// writeByte(0xFBFF, -32);
|
||||
dump_eeprom();
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define ms_since(ms,num) (((uint16_t)get_ms() - (ms)) >= num ? (1|(ms=(uint16_t)get_ms())):0)
|
||||
|
||||
int16_t main(void) {
|
||||
|
||||
data uint8_t secbyte = readByte(0xFBFF);
|
||||
data uint8_t i = 0;
|
||||
data uint16_t last_ms = get_ms();
|
||||
data uint16_t ms_heart;
|
||||
|
|
@ -203,7 +103,7 @@ int16_t main(void) {
|
|||
u2f_printlx("lx:",1,l);
|
||||
run_tests();
|
||||
test_eeprom();
|
||||
u2f_printb("security_byte: ",1,secbyte);
|
||||
|
||||
while (1) {
|
||||
|
||||
if (ms_since(ms_heart,500))
|
||||
|
|
|
|||
|
|
@ -4,28 +4,24 @@
|
|||
* Created on: Feb 14, 2016
|
||||
* Author: Conor
|
||||
*/
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <SI_EFM8UB1_Register_Enums.h>
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include "app.h"
|
||||
#include "bsp.h"
|
||||
#include "i2c.h"
|
||||
#include "atecc508a.h"
|
||||
#include "eeprom.h"
|
||||
#include "tests.h"
|
||||
|
||||
|
||||
#ifdef ENABLE_TESTS
|
||||
|
||||
|
||||
static void PRINT(const char * fmt, ...)
|
||||
static void PRINT(const char * s)
|
||||
{
|
||||
va_list args;
|
||||
|
||||
va_start(args,fmt);
|
||||
vsprintf(appdata.hidmsgbuf, fmt, args);
|
||||
va_end(args);
|
||||
|
||||
u2f_write_s(appdata.hidmsgbuf);
|
||||
u2f_prints(s);
|
||||
u2f_prints("\r\n");
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -63,7 +59,7 @@ static int test_sha()
|
|||
#define test_sha(x)
|
||||
#endif
|
||||
|
||||
#ifdef TEST_EEPROM
|
||||
#ifdef TEST_ATECC_EEPROM
|
||||
|
||||
static void slot_dump(void* slot)
|
||||
{
|
||||
|
|
@ -104,7 +100,7 @@ static void key_dump(void* slot)
|
|||
flush_messages();
|
||||
}
|
||||
|
||||
static int test_eeprom()
|
||||
static int test_atecc_eeprom()
|
||||
{
|
||||
uint8_t buf[7];
|
||||
uint16_t c1,c2,c3,c4;
|
||||
|
|
@ -188,7 +184,7 @@ static int test_eeprom()
|
|||
return 0;
|
||||
}
|
||||
#else
|
||||
#define test_eeprom(x)
|
||||
#define test_atecc_eeprom(x)
|
||||
#endif
|
||||
|
||||
#ifdef TEST_KEY_SIGNING
|
||||
|
|
@ -222,6 +218,74 @@ int test_key_signing()
|
|||
#define test_key_signing(x)
|
||||
#endif
|
||||
|
||||
#ifdef TEST_EFM8UB1_EEPROM
|
||||
|
||||
void dump_eeprom()
|
||||
{
|
||||
// 0xF800 - 0xFB7F
|
||||
uint16_t i = 0xF800;
|
||||
uint8_t eep;
|
||||
for (; i <= 0xF800 + 300; i++)
|
||||
{
|
||||
eeprom_read(i,&eep,1);
|
||||
u2f_putb(eep);
|
||||
}
|
||||
}
|
||||
|
||||
int8_t test_efm8ub1_eeprom()
|
||||
{
|
||||
uint16_t crc = 0;
|
||||
uint8_t secbyte;
|
||||
int8_t i;
|
||||
char k[] = "\x55\x66\x77\x88";
|
||||
char buf[4];
|
||||
|
||||
eeprom_read(0xFBFF,&secbyte,1);
|
||||
|
||||
u2f_printb("security_byte: ",1,secbyte);
|
||||
|
||||
if (secbyte == 0xff)
|
||||
{
|
||||
eeprom_erase(0xFBC0);
|
||||
i = -32;
|
||||
eeprom_write(0xFBFF, &i, 1);
|
||||
u2f_prints("eeprom_write\r\n");
|
||||
}
|
||||
|
||||
eeprom_write(KEYHANDLES_START + 0, k, 4);
|
||||
eeprom_write(KEYHANDLES_START + 4, k, 4);
|
||||
eeprom_write(KEYHANDLES_START + 8, k, 4);
|
||||
eeprom_write(KEYHANDLES_START + 12, k, 4);
|
||||
|
||||
eeprom_read(KEYHANDLES_START + 0,buf,4);
|
||||
for(i=0; i < 4; i++) crc = feed_crc(crc, buf[i]);
|
||||
dump_hex(buf,4);
|
||||
dump_hex(k,4);
|
||||
eeprom_read(KEYHANDLES_START + 4,buf,4);
|
||||
for(i=0; i < 4; i++) crc = feed_crc(crc, buf[i]);
|
||||
dump_hex(buf,4);
|
||||
dump_hex(k,4);
|
||||
eeprom_read(KEYHANDLES_START + 8,buf,4);
|
||||
for(i=0; i < 4; i++) crc = feed_crc(crc, buf[i]);
|
||||
dump_hex(buf,4);
|
||||
dump_hex(k,4);
|
||||
eeprom_read(KEYHANDLES_START + 12,buf,4);
|
||||
for(i=0; i < 4; i++) crc = feed_crc(crc, buf[i]);
|
||||
dump_hex(buf,4);
|
||||
dump_hex(k,4);
|
||||
|
||||
u2f_printx("crc: ", 1, crc);
|
||||
|
||||
if (crc == 0xd1e8)
|
||||
return 0;
|
||||
return -1;
|
||||
}
|
||||
|
||||
#else
|
||||
#define test_efm8ub1_eeprom(x)
|
||||
#endif
|
||||
|
||||
|
||||
void run_tests()
|
||||
{
|
||||
int rc;
|
||||
|
|
@ -235,9 +299,9 @@ void run_tests()
|
|||
PRINT("--- SHA TEST FAILED %d ---\r\n",rc);
|
||||
#endif
|
||||
|
||||
#ifdef TEST_EEPROM
|
||||
PRINT("--- STARTING EEPROM TEST ---\r\n");
|
||||
rc = test_eeprom();
|
||||
#ifdef TEST_ATECC_EEPROM
|
||||
PRINT("--- STARTING ATECC EEPROM TEST ---\r\n");
|
||||
rc = test_atecc_eeprom();
|
||||
if (rc == 0)
|
||||
PRINT("--- EEPROM TEST SUCCESS ---\r\n");
|
||||
else
|
||||
|
|
@ -253,6 +317,16 @@ void run_tests()
|
|||
PRINT("--- KEY SIGNING FAILED %d ---\r\n",rc);
|
||||
#endif
|
||||
|
||||
#ifdef TEST_EFM8UB1_EEPROM
|
||||
PRINT("--- STARTING EFM8UB1 EEPROM TEST ---\r\n");
|
||||
rc = test_efm8ub1_eeprom();
|
||||
if (rc == 0)
|
||||
PRINT("--- EFM8UB1 EEPROM SUCCESS ---\r\n");
|
||||
else
|
||||
PRINT("--- EFM8UB1 EEPROM FAILED ---\r\n");
|
||||
#endif
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -8,10 +8,11 @@
|
|||
#ifndef TESTS_H_
|
||||
#define TESTS_H_
|
||||
|
||||
//#define ENABLE_TESTS
|
||||
#define ENABLE_TESTS
|
||||
|
||||
//#define TEST_SHA
|
||||
//#define TEST_EEPROM
|
||||
//#define TEST_ATECC_EEPROM
|
||||
#define TEST_EFM8UB1_EEPROM
|
||||
//#define TEST_KEY_SIGNING // requires key and locked eeprom
|
||||
|
||||
#ifdef ENABLE_TESTS
|
||||
|
|
|
|||
Ładowanie…
Reference in New Issue