Merge branch 'master' of github.com:r0ket/r0ket

This commit is contained in:
Sebastian 'iggy' Steuer 2011-07-19 02:43:56 +02:00
commit eb5dd5cc9c
6 changed files with 72 additions and 14 deletions

View File

@ -37,6 +37,21 @@ void htonlp(uint32_t *v, uint8_t n)
}
}
void xxtea_cbcmac(uint32_t mac[4], uint32_t *data,
uint32_t len, uint32_t const key[4])
{
if( len & 0x03 )
return;
mac[0]=0;mac[1]=0;mac[2]=0;mac[3]=0;
for(int i=0; i<len;){
mac[0] ^= data[i++];
mac[1] ^= data[i++];
mac[2] ^= data[i++];
mac[3] ^= data[i++];
xxtea_encode_words(mac, 4, key);
}
}
#define DELTA 0x9e3779b9
#define MX (((z>>5^y<<2) + (y>>3^z<<4)) ^ ((sum^y) + (k[(p&3)^e] ^ z)))

View File

@ -1,6 +1,8 @@
#ifndef _XXTEA_H_
#define _XXTEA_H_
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]);

View File

@ -10,11 +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){
void execute_file (const char * fname, uint8_t checksignature, uint8_t decode){
FRESULT res;
FIL file;
UINT readbytes;
@ -27,6 +32,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();
@ -42,6 +48,26 @@ void execute_file (const char * fname){
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();
@ -60,6 +86,6 @@ void executeSelect(char *ext){
filename[2]=0;
if( selectFile(filename+2,ext) == 0)
execute_file(filename);
execute_file(filename,0,0);
};

View File

@ -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, uint8_t decode);
void executeSelect(char *ext);
#endif

View File

@ -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;
}

View File

@ -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,