From 643e8365aab52d92e6d5a004062d0d228955f9e4 Mon Sep 17 00:00:00 2001 From: schneider Date: Tue, 19 Jul 2011 00:19:50 +0200 Subject: [PATCH 1/4] openbeacon: increment seq counter on boot --- firmware/funk/openbeacon.c | 30 +++++++++++++++++++++--------- firmware/funk/openbeacon.h | 7 +++++-- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/firmware/funk/openbeacon.c b/firmware/funk/openbeacon.c index 835d89d..4c1810b 100644 --- a/firmware/funk/openbeacon.c +++ b/firmware/funk/openbeacon.c @@ -10,11 +10,22 @@ const uint32_t key[4] = { 0xB4595344,0xD3E119B6,0xA814D0EC,0xEFF5A24E }; const uint8_t useencryption = 1; const uint8_t mac[5] = {1,2,3,2,1}; -uint32_t oid = 0; -uint32_t seq = 0; -uint8_t strength = 0; +volatile uint32_t oid = 0; +volatile uint32_t seq = 0; +volatile uint8_t strength = 0; -void openbeaconSave() +void openbeaconShutdown(void) +{ + openbeaconSave(seq); +} + +void openbeaconSaveBlock(void) +{ + + openbeaconSave(seq + OPENBEACON_SAVE + 1); +} + +void openbeaconSave(uint32_t s) { FIL file; BYTE buf[4]; @@ -23,7 +34,7 @@ void openbeaconSave() if( f_open(&file, "beacon", FA_OPEN_ALWAYS|FA_WRITE) ) return; - uint32touint8p(seq, buf); + uint32touint8p(s, buf); if( f_write(&file, buf, 4, &readbytes) ) return; @@ -37,7 +48,7 @@ void openbeaconRead() BYTE buf[4]; UINT readbytes; - if( f_open(&file, "beacon", FA_OPEN_EXISTING|FA_READ) ) + if( f_open(&file, "beacon.cfg", FA_OPEN_EXISTING|FA_READ) ) return; if( f_read(&file, buf, 4, &readbytes) ) @@ -53,6 +64,7 @@ void openbeaconSetup(uint32_t id) oid = id; strength = 0; openbeaconRead(); + openbeaconSaveBlock(); } uint8_t openbeaconSendPacket(uint32_t id, uint32_t seq, @@ -80,11 +92,11 @@ uint8_t openbeaconSend(void) nrf_set_strength(strength); nrf_set_tx_mac(sizeof(mac), mac); - status = openbeaconSendPacket(oid, seq++, 0xFF, strength++); + status = openbeaconSendPacket(oid, seq, 0xFF, strength++); if( strength == 4 ) strength = 0; - if( seq % OPENBEACON_SAVECOUNTER == 0 ) - openbeaconSave(); + if( seq++ & OPENBEACON_SAVE == OPENBEACON_SAVE ) + openbeaconSaveBlock(); return status; } diff --git a/firmware/funk/openbeacon.h b/firmware/funk/openbeacon.h index 2d2a539..85d6469 100644 --- a/firmware/funk/openbeacon.h +++ b/firmware/funk/openbeacon.h @@ -5,8 +5,11 @@ #include "funk/nrf24l01p.h" #include "basic/byteorder.h" -#define OPENBEACON_SAVECOUNTER (1024*8) -void openbeaconSave(); +#define OPENBEACON_SAVE 0xFFFF + +void openbeaconShutdown(void); +void openbeaconSaveBlock(void); +void openbeaconSave(uint32_t s); void openbeaconRead(); void openbeaconSetup(uint32_t id); uint8_t openbeaconSendPacket(uint32_t id, uint32_t ctr, From 27412ef03d8f64215fadf0a7c68cb0d81755c0f6 Mon Sep 17 00:00:00 2001 From: schneider Date: Tue, 19 Jul 2011 00:51:07 +0200 Subject: [PATCH 2/4] basic: added cbc-mac with xxtea for messages with n*4 words --- firmware/basic/xxtea.c | 14 ++++++++++++++ firmware/basic/xxtea.h | 1 + 2 files changed, 15 insertions(+) diff --git a/firmware/basic/xxtea.c b/firmware/basic/xxtea.c index c768d85..91e0316 100644 --- a/firmware/basic/xxtea.c +++ b/firmware/basic/xxtea.c @@ -37,6 +37,20 @@ void htonlp(uint32_t *v, uint8_t n) } } +void xxtea_cbcmac(uint32_t mac[4], uint32_t *data, uint32_t len, uint32_t key[4]) +{ + if( len & 0x03 ) + return; + mac[0]=0;mac[1]=0;mac[2]=0;mac[3]=0; + for(int i=0; i>5^y<<2) + (y>>3^z<<4)) ^ ((sum^y) + (k[(p&3)^e] ^ z))) diff --git a/firmware/basic/xxtea.h b/firmware/basic/xxtea.h index e8b99c9..4a08ce6 100644 --- a/firmware/basic/xxtea.h +++ b/firmware/basic/xxtea.h @@ -1,6 +1,7 @@ #ifndef _XXTEA_H_ #define _XXTEA_H_ +void xxtea_cbcmac(uint32_t mac[4], uint32_t *data, uint32_t len, uint32_t key[4]); void xxtea_encode_words(uint32_t *v, int n, uint32_t const k[4]); void xxtea_decode_words(uint32_t *v, int n, uint32_t const k[4]); From 43eddd123bad829330a572c4aa42a7b50acb3de1 Mon Sep 17 00:00:00 2001 From: schneider Date: Tue, 19 Jul 2011 01:12:53 +0200 Subject: [PATCH 3/4] filesystem: check signature of loadables with cbc-mc --- firmware/filesystem/execute.c | 19 +++++++++++++++++-- firmware/filesystem/execute.h | 2 +- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/firmware/filesystem/execute.c b/firmware/filesystem/execute.c index 3232f02..071b910 100644 --- a/firmware/filesystem/execute.c +++ b/firmware/filesystem/execute.c @@ -10,11 +10,14 @@ #include "filesystem/ff.h" #include "filesystem/select.h" + +const uint32_t signature_key[4] = {0,0,0,0}; + extern void * sram_top; /**************************************************************************/ -void execute_file (const char * fname){ +void execute_file (const char * fname, uint8_t checksignature){ FRESULT res; FIL file; UINT readbytes; @@ -27,6 +30,7 @@ void execute_file (const char * fname){ dst=(void (*)(void)) 0x10001800; res=f_open(&file, fname, FA_OPEN_EXISTING|FA_READ); + //lcdPrint("open: "); //lcdPrintln(f_get_rc_string(res)); //lcdRefresh(); @@ -41,7 +45,18 @@ void execute_file (const char * fname){ if(res){ return; }; + + if( checksignature ){ + uint32_t mac[4]; + uint32_t *data = (uint32_t*)dst; + uint32_t len = readbytes/4; + xxtea_cbcmac(mac, (uint32_t*)dst, len-4, signature_key); + if( data[len-4] != mac[0] || data[len-3] != mac[1] + || data[len-2] != mac[2] || data[len-1] != mac[3] ){ + return; + } + } //lcdPrintInt(readbytes); //lcdPrintln(" bytes"); //lcdRefresh(); @@ -60,6 +75,6 @@ void executeSelect(char *ext){ filename[2]=0; if( selectFile(filename+2,ext) == 0) - execute_file(filename); + execute_file(filename,0); }; diff --git a/firmware/filesystem/execute.h b/firmware/filesystem/execute.h index 7242d6d..2b4b0f1 100644 --- a/firmware/filesystem/execute.h +++ b/firmware/filesystem/execute.h @@ -1,7 +1,7 @@ #ifndef _EXECUTE_H_ #define _EXECUTE_H_ -void execute_file (const char * fname); +void execute_file (const char * fname, uint8_t checksignature); void executeSelect(char *ext); #endif From b49a49f31c0aefcacd609f1554e0c3368f1c1fbf Mon Sep 17 00:00:00 2001 From: schneider Date: Tue, 19 Jul 2011 01:21:05 +0200 Subject: [PATCH 4/4] filesystem: add option to decode loadables --- firmware/basic/xxtea.c | 3 ++- firmware/basic/xxtea.h | 3 ++- firmware/filesystem/execute.c | 17 ++++++++++++++--- firmware/filesystem/execute.h | 2 +- 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/firmware/basic/xxtea.c b/firmware/basic/xxtea.c index 91e0316..9ae81f8 100644 --- a/firmware/basic/xxtea.c +++ b/firmware/basic/xxtea.c @@ -37,7 +37,8 @@ void htonlp(uint32_t *v, uint8_t n) } } -void xxtea_cbcmac(uint32_t mac[4], uint32_t *data, uint32_t len, uint32_t key[4]) +void xxtea_cbcmac(uint32_t mac[4], uint32_t *data, + uint32_t len, uint32_t const key[4]) { if( len & 0x03 ) return; diff --git a/firmware/basic/xxtea.h b/firmware/basic/xxtea.h index 4a08ce6..da93394 100644 --- a/firmware/basic/xxtea.h +++ b/firmware/basic/xxtea.h @@ -1,7 +1,8 @@ #ifndef _XXTEA_H_ #define _XXTEA_H_ -void xxtea_cbcmac(uint32_t mac[4], uint32_t *data, uint32_t len, uint32_t key[4]); +void xxtea_cbcmac(uint32_t mac[4], uint32_t *data, + uint32_t len, uint32_t const key[4]); void xxtea_encode_words(uint32_t *v, int n, uint32_t const k[4]); void xxtea_decode_words(uint32_t *v, int n, uint32_t const k[4]); diff --git a/firmware/filesystem/execute.c b/firmware/filesystem/execute.c index 071b910..ee387b0 100644 --- a/firmware/filesystem/execute.c +++ b/firmware/filesystem/execute.c @@ -10,14 +10,16 @@ #include "filesystem/ff.h" #include "filesystem/select.h" +#include "basic/xxtea.h" const uint32_t signature_key[4] = {0,0,0,0}; +const uint32_t decode_key[4] = {0,0,0,0}; extern void * sram_top; /**************************************************************************/ -void execute_file (const char * fname, uint8_t checksignature){ +void execute_file (const char * fname, uint8_t checksignature, uint8_t decode){ FRESULT res; FIL file; UINT readbytes; @@ -45,18 +47,27 @@ void execute_file (const char * fname, uint8_t checksignature){ if(res){ return; }; + + if( decode || checksignature ) + if( readbytes & 0x03 ) + return; if( checksignature ){ uint32_t mac[4]; uint32_t *data = (uint32_t*)dst; uint32_t len = readbytes/4; - xxtea_cbcmac(mac, (uint32_t*)dst, len-4, signature_key); if( data[len-4] != mac[0] || data[len-3] != mac[1] || data[len-2] != mac[2] || data[len-1] != mac[3] ){ return; } } + + if( decode ){ + uint32_t *data = (uint32_t*)dst; + uint32_t len = readbytes/4; + xxtea_decode_words(data, len, decode_key); + } //lcdPrintInt(readbytes); //lcdPrintln(" bytes"); //lcdRefresh(); @@ -75,6 +86,6 @@ void executeSelect(char *ext){ filename[2]=0; if( selectFile(filename+2,ext) == 0) - execute_file(filename,0); + execute_file(filename,0,0); }; diff --git a/firmware/filesystem/execute.h b/firmware/filesystem/execute.h index 2b4b0f1..8c52cae 100644 --- a/firmware/filesystem/execute.h +++ b/firmware/filesystem/execute.h @@ -1,7 +1,7 @@ #ifndef _EXECUTE_H_ #define _EXECUTE_H_ -void execute_file (const char * fname, uint8_t checksignature); +void execute_file (const char * fname, uint8_t checksignature, uint8_t decode); void executeSelect(char *ext); #endif